简体   繁体   English

Java-使死线程恢复活动

[英]Java - Bring the dead thread back to alive

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? 还是他只是检查我对我答案的信心?

Dead state : A thread can be considered dead when its run() method completes. 死状态 :当线程的run()方法完成时,可以将其视为死状态。 If any thread comes on this state that means it cannot ever run again. 如果有任何线程进入此状态,则意味着该线程无法再运行。

您可以使用旧的Thread对象创建一个新的对象,这几乎是相同的:

(new Thread(oldThread)).start();

I think he was trying to prod you about your knowledge of ThreadPools. 我认为他正在尝试让您了解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. 正如您所说,无法恢复线程,但是使用线程池或更新的执行程序框架,我们可以减少对象创建的开销。

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. 即使线程没有死,它也睡着了,您确实将它恢复了生命。

Yeah I had similar question asked. 是的,我问过类似的问题。 When I said no, he asked me whether I was familiar with cached Threadpool concept. 当我拒绝时,他问我是否熟悉缓存的Threadpool概念。

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. 他还用类似的问题轰炸了我,例如Weblogic是否使用相同的线程来服务所有请求,还是继续为每个请求创建新线程。

I had seen weblogic logs and I had seen Thread - 0 being printed out and performing multiple task. 我看过weblogic日志,也看过Thread-0被打印出来并执行多项任务。 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. 我不确定weblogic是否在池中创建了一个具有相同名称的新线程,或者它是否是同一线程,所以我只是想通过回答某个问题以使他离开这个问题,试图将面试官切换到另一个问题。

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. 我看到siva在面试过程中问了这个问题,我猜他在印度,所以我确切地知道面试官在自己进行了这么多面试之后正在做什么。 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. 休息在siva上,他想选择要回答的答案。 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. 但是如果他们用其他东西轰炸您,例如weblogic重用线程如何为他们提供上述答案。 Rest is all your wish. 休息就是你的全部愿望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM