Thread lifecycle on ThreadDeath

蹲街弑〆低调 提交于 2019-12-11 02:37:43

问题


I wanted to start a computation and terminate it on user Cancel. You say that calling Thead.stop() unlocks all monitors and raises ThreadDeath exception. This means that finally is still called.

Suppose I do not care about the locks but want to release the resources allocated so far. I would like the canceller to know/wait until thread has finished released its allocations. Suppose the finally section has an infinite loop in the finally section. Will thread.join succeed to that thread?


回答1:


It depends on what code the thread is executing at the point where ThreadDeath is thrown. For instance, if your thread is stopped just as it happens to reach the top of a finally block, then you could have a situation where ThreadDeath is thrown after the block is entered but before the resource is closed, and that finally block would not be completed.

It would be a lot less error-prone to use interruption, which lets the thread being interrupted have full control over how it finishes up and closes its resources.



来源:https://stackoverflow.com/questions/37195300/thread-lifecycle-on-threaddeath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!