Question

How do I kill a thread? The stop() method is deprecated in JDK1.2, so how do I shut it down?

Answer

The API documentation for JDK1.2 discusses an alternate mechanism for stopping threads, by having them continually poll a boolean flag to see if they should terminate of their own accord. This is an option, but if you have threads that become deadlocked or stall waiting for I/O, sometimes you'll have to kill them the hard way.

Why shouldn't you normally use the Thread.stop() method? Well, it is deprecated as of JDK1.2 because it can potentially leave the system in an unsafe state. If a thread
had a lock on an object (within a synchronized block), it might not release the object lock, causing problems at a later time. Note that this problem can affect older JVM implementations as well, JDK1.02 and JDK1.1 are not immune.

So while you shouldn't use Thread.stop() unless absolutely necessary, it is an acceptable way to kill a thread if it becomes stalled and you really need to shut it down fast.


Back