Reputation: 7740
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?
Upvotes: 6
Views: 3377
Reputation: 1
Yeah I had similar question asked. When I said no, he asked me whether I was familiar with cached Threadpool concept.
He said threads are returned from the pool and they service multiple request how do they do that if it can be started only once. He also bombarded me with similar question like does Weblogic use same threads to service all the request or it keeps on creating new threads for each request.
I had seen weblogic logs and I had seen Thread - 0 being printed out and performing multiple task. I was not sure if weblogic created a new thread in the pool with the same name or it was the same thread, so I just tried to switch the interviewer to another question by answering in a way to take him further from this question.
Answer to this question is, We actually provide runnable objects to the threads and it does not terminate for example:
while(doINeedToKeepRunning()){
if(hasMoreTasks()){
getFirstTask().run();.
}
else
{
waitForOtherTasks();
}
}
Hope this answers your question. So such threads have not terminated they just wait for runnable or callable objects.
I know this sounds stupid. But this is the answer they are looking in a interview. I saw siva asked this question in the context of interview and i am guessing he is in INDIA, so I know exactly what the interviewer is trying to do after giving so much of interview myself. I have tried all the other answer but they do not stop un til they get this answer. Rest is on siva which answer he wants to pick to answer them. The point of asking such pointless question is to confuse the interviewee and make him change his answer and test his knowledge in thread pool implementation.
As we all know once a thread is terminated it cannot be run again. So stick to what you know already. No Never a dead thread cannot be bought to life again. but if they bombard you with other things of like how weblogic re-use threads give them the above answer. Rest is all your wish.
Upvotes: -2
Reputation: 8774
It depends on what you mean by a Dead State. If you had a Thread that was 'sleeping', it would effectively not be doing any work, so you could 'revive' it and make it continue running from this point onwards. Even though the Thread wasn't dead, it was asleep, and you did bring it back to life.
Upvotes: 0
Reputation: 23250
You can use the old Thread object to create a new one, which is pretty much the same thing:
(new Thread(oldThread)).start();
Upvotes: 4
Reputation: 3287
I think he was trying to prod you about your knowledge of ThreadPools. The thread cannot be revived as you have said, but using thread pools or the newer executor framework we can reduce the object creation overhead.
Upvotes: 3
Reputation: 853
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.
Upvotes: 5