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?
Once a thread enters dead state it cannot be restarted.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With