Mab
Mab

Reputation: 412

Restarting same Java Thread

OK anyone please, I asked this question as an Android question but I realized I was able to recreate similar scenario on a pure Java Application.

I created a Thread class as an inner class and override its run() method

    Thread aaa = new Thread(){
        public void run() {
            for(int i=0;i<5;i++){
                System.out.println(getId()+" <-"+i);
                try {
                    Thread.sleep(1000 * 1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
    };

In my Main method, I started that Thread and after that slept for 6 seconds (the idea the is that the Thread would have finished its run method by time main wakes up back) and I then attempt to start same thread again

public static void main(String[] args) {
        Test test = new Test();
        //Start the thread
        test.aaa.start();
        

        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            
            e.printStackTrace();
        }
        //Start same dead thread again      
        test.aaa.start();
    }

On my Ubuntu machine starting the Thread again in that last line gives IllegalThreadStateException which was what I considered the norms. Output looks like:

9 <-0
9 <-1
9 <-2
9 <-3
9 <-4
Exception in thread "main" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:708)
    at Test.main(Test.java:15)

But on My Android device its running same Thread over - over again, Output looks like :



50023 <-0
50023 <-1
50023 <-2
50023 <-3
50023 <-4
50023 <-0
50023 <-1
50023 <-2
50023 <-3
50023 <-4

I don't understand how same Thread is started again, atleast the Id proves that.

Upvotes: 0

Views: 52

Answers (2)

jurez
jurez

Reputation: 4667

As per Java specification, you can only start a thread once. In other words, you must not start a thread which has already been started.

The fact that it happens to work on Android (or anywhere else) is irrelevant. Even if it appears to work, it is still wrong and it can cause unpredictable results.

If you need to run a thread again, create a new one.

As an alternative, you can use an ExecutorService, but that's a different topic.

Upvotes: 1

Sa&#239;d
Sa&#239;d

Reputation: 9398

I think the source of your confusion is because you are testing on two similar but not identical runtime environments. In other words, there are two different runtime environments or virtual machines (aka VM) at play here:

The behaviour you've described where the 2nd invocation of thread.start() led to an IllegalThreadStateException is the correct behaviour on a standard Java runtime environment based on the JVM.

The behaviour you're experiencing where the code doesn't crash on your device is because the Java runtime environment on your Android device is different -- it does not fully conform to the JVM spec.

Upvotes: 2

Related Questions