19

What are all the different possibilities to bring the dead thread back to runnable state.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55

7 Answers7

54

Thread lifecycle image

If you look at the Thread Life Cycle Image, there is no way you can go back to new position once your thread has terminated.

So there is no way to bring back the dead thread to runnable state,instead you should create a new Thread instance.

Community
  • 1
  • 1
Nargis
  • 4,687
  • 1
  • 28
  • 45
14

From the JavaDocs...

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

You'll have to start a brand new instance.

Preferably, the actions you want to execute should be wrapped up in a Runnable interface, that way you can simply pass the Runnable to a new instance of Thread

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
12

I guess you extended the Thread class and you have overridden the run method. If you do this you are tying the runnable code to the Thread's lifecycle. Since a Thread can not be restarted you have to create a new Thread everytime. A better practice is to separate the code to run in a thread from a Thread's lifecycle by using the Runnable interface.

Just extract the run method in a class that implements Runnable. Then you can easily restart it.

For example:

 public class SomeRunnable implements Runnable {

      public void run(){
         ... your code here
      }
 }

 SomeRunnable someRunnable = new SomeRunnable();
 Thread thread = new Thread(someRunnable);
 thread.start();

 thread.join(); // wait for run to end

 // restart the runnable
 thread = new Thread(someRunnable);
 thread.start();

This practice makes it also easy if you need to remember the previous run state.

public class SomeRunnable implements Runnable {

      private int runs = 0;

      public void run(){
          runs++;
          System.out.println("Run " + runs + " started");
      }
 }

PS: Use a java.util.concurrent.Executor to execute Runnables. This will decouple thread management from execution.

 Executor executor = Executors.newSingleThreadExecutor();

 ...

 SomeRunnable someRunnable = new SomeRunnable();
 executor.execute(someRunnable);

Take a look at Executor Interfaces

René Link
  • 48,224
  • 13
  • 108
  • 140
5

The thread is a separate light weight process which executes independently irrespective of other threads. Once its execution is complete, there exists no means to restart it.

Ashish Babu
  • 1,167
  • 9
  • 20
4

The other obvious solution is: if you need the thread functionality many times, don't let the thread die. Instead of letting it exit, and so terminate itself, shove in a while(true) loop with a suitable wait at the top. You can then make it 'restart' its work by signaling it.

This is much quicker, safer and more efficient than continually creating/terminating/destroying threads.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Martin James
  • 24,453
  • 3
  • 36
  • 60
3

When the execution of run() method is over, as the job it is meant is done, it is brought to dead state. It is done implicitly by JVM. In dead state, the thread object is garbage collected. It is the end of the life cycle of thread. Once a thread is removed, it cannot be restarted again (as the thread object does not exist).

enter image description here

Read more From Here about life cycle of Threads.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
3

Thread has many different state through out its life.

1 Newborn State

2 Runnable State

3 Running State

4 Blocked State

5 Dead State

Thread should be in any one state of above and it can be move from one state to another by different methods and ways.

enter image description here

When a thread is completed executing its run() method the life cycle of that particular thread is end.

We can kill thread by invoking stop() method for that particular thread and send it to be in Dead State.

Dhrumil Shah
  • 736
  • 6
  • 15