线程突然终止

Abrupt termination of threads

在 C 和 C++ 中,当您调用 TerminateThread (Windows) 或 pthread_kill (Linux) 时,它可能会泄漏之前未释放的已分配资源线程突然死亡。这些是 OS 强制终止线程的特定调用。发生这种情况时,不会调用 C++ 中的析构函数。

当您调用 Thread.stop() 时,在 Java 中是否会发生同样的事情? JVM 是否在线程突然终止之前收集线程生命周期内分配的所有对象?或者它也会泄漏吗?

当JVM终止线程时,相应的堆栈和引用将被清除,因此这些引用独占引用的对象将成为垃圾收集的候选对象。

Thread.stop() 已弃用,您不应该调用它。 From the doc:

Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced

所以这与资源分配问题无关,而是对象完整性问题。

检查此页面 Java 8 http://docs.oracle.com/javase/8/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

Why is Thread.stop deprecated?

Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions, ThreadDeath kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.