Java - Bring a dead thread back to life

In one of my interviews (several years ago) the following question was asked:

What are all the possibilities / ways to bring a dead stream back to life (Runnable State)

I defended that there is no way. But he made me think. Are there really any options? Or did he just test my confidence in my answers?

+6
source share
5 answers

I think he was trying to push you to your knowledge in ThreadPools. A stream cannot be restored, as you said, but by using thread pools or a new artist structure, we can reduce the overhead of creating objects.

+3
source

Dead state : A thread can be considered dead when its run () method is complete. If any thread enters this state, it means that it can no longer work.

+5
source

You can use the old Thread object to create a new one, which is almost the same:

(new Thread(oldThread)).start(); 
+4
source

It depends on what you mean by Dead State. If you had a Thread that was β€œsleeping,” it would actually not do any work, so you could β€œrevive” it and make it continue to work from now on. Although the thread was not dead, she was asleep, and you brought her back to life.

0
source

Yes, I had a similar question. When I said no, he asked me if I was familiar with Threadpool's cache concept.

He said that threads are returned from the pool and they serve several requests, as they do if they can only be started once. He also overturned me with a similar question, how Weblogic uses the same threads to serve the entire request or continues to create new threads for each request.

I saw the logs of magazines, and I saw how Thread-0 printed and performed several tasks. I was not sure if the weblogic created a new thread in the pool with the same name or was the same thread, so I just tried to switch the interviewer to another question, answering in such a way as to distract him from this question.

The answer to this question: we actually provide running objects for threads, and it does not end, for example:

 while(doINeedToKeepRunning()){ if(hasMoreTasks()){ getFirstTask().run();. } else { waitForOtherTasks(); } } 

Hope this answers your question. Thus, such flows do not stop, they just wait for objects to be launched or called.

I know that sounds silly. But this is the answer they are looking for in an interview. I saw that Shiva asked this question in the context of the interview, and I guess he is in India, so I know exactly what the interviewer is trying to do after he gave me so many interviews. I tried all the other answers, but they do not stop until they get this answer. Rest on the shiva who answers, he wants to choose to answer them. The task of such a meaningless question is to confuse the interlocutor and make him change his answer and test his knowledge in the implementation of the thread pool.

As we all know, when a thread is completed, it cannot be started again. So stick to what you already know. No Never a dead thread can be bought again. but if they bombard you with other things, like the way weblogic reuses threads, gives them the above answer. Rest is all you need.

-2
source

Source: https://habr.com/ru/post/911848/


All Articles