Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Bring the dead thread back to alive

Tags:

java

The following question was asked in one of my interview (few years back)

What are all the possibilities/ways to bring the dead thread back to alive(Runnable State)

I have defended that there is no way. But he was pressing me to think. Are there any options available really? Or Was he just checking my confidence on my answers?

like image 938
Sivasubramaniam Arunachalam Avatar asked Mar 28 '12 10:03

Sivasubramaniam Arunachalam


People also ask

How do you start a dead thread again?

Once a thread enters dead state it cannot be restarted.

How do you make a thread in Java can a dead thread be started again?

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.

Can we run the dead thread in Java?

Once the thread completes its run() method and dead, it cannot be brought back to thread of execution or even to runnable state. Invoking start() method on a dead thread causes runtime exception.

Can you start again same thread which was stopped?

No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.


1 Answers

Dead state : A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.

like image 172
Riddhish.Chaudhari Avatar answered Nov 01 '22 06:11

Riddhish.Chaudhari