Why is Thread.stop not working in situations where Thread.interrupt is not working?

Oracle's official position Thread.stop()is that it cannot be used. Among other arguments, they write :

It should be noted that in all situations when the wait thread does not respond to Thread.interrupt, it also does not respond to Thread.stop.

But I don’t get it. If a thread is actively working on something (and not just waiting or blocking an external resource) and does not explicitly check the interrupt flag, will Thread.interrupt()it not do anything while Thread.stop()it still works (throw ThreadDeath)?

+3
source share
4 answers

. - ( ) , Thread.interrupt() , Thread.stop() ( throw ThreadDeath)?

, . , , , . , :

  • -, JVM stop, interrupt.

  • , , ThreadDeath, , , .

+4

Thread.stop , . , . Thread.stop(), () .

, stop() , () (, ): ineterrupt .

POSIX, SIGUSR2 () , /. : . . , : stop() Throwable , Java-. , .

, , , .

, Thread.uncaughtExceptionHandler, , .. : .

+2

, , , ThreadDeath , interupt .

Consider this code running in a thread:

public void sellItem(Store s) {
   synchronized (s) {
       if (s.itemsAvailable > 0) {
           s.itemsAvailable--;
           s.itemsSold++;
       }
   }
}

If ThreadDeath is called after s.itemsAvailable--, the Store object remains in an inconsistent state. This code, on the other hand, is safe:

public void sellLoop(Store s) {
    while (!Thread.interrupted())
       sellItem(s);
}

Source: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#stop%28%29

They say that Thread.stop()it will not work, because (I think) throwable can be caught and ignored.

0
source

if the JVM is too busy to interrupt the thread, it is also too busy to kill it.

-1
source

All Articles