且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ThreadDeath上的线程生命周期

更新时间:2023-01-25 17:25:45

这取决于线程在抛出ThreadDeath时执行的代码.例如,如果您的线程恰好到达了finally块的顶部而停止了,那么您可能会遇到这样一种情况,即在进入该块之后但在关闭资源之前会抛出ThreadDeath,而不会关闭该finally块完全的.

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.