Go from: http://polaris.blog.51cto.com/1146394/372146
in Java, you used the Stop method to stop a thread, however, the method is inherently unsafe and thus has been discarded (deprecated). So how should we end a process? This is explained in detail in the official document: "Why is it not in favour of using Thread.stop, Thread.Suspend and Thread.Resume?" 》。 Description of the Stop method is referenced here: 1. Why is thread.stop deprecated? Because it is inherently unsafe. Stopping a thread causes it to unlock all of 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 this monitors were in a inconsistent state, and the other threads could now view the SE objects in a inconsistent state. Such objects are said to be damaged. When threads operate in damaged objects, arbitrary behavior can result. This behavior may is subtle and difficult to detect, or it may is pronounced. Unlike other unchecked exceptions, Threaddeath kills threads; Thus, the user has no warning, is corrupted. The corruption can manifest itself at the actual damage occurs, even hours or days in the future. Probably means: because the partyThe law is inherently unsafe. Stopping a thread frees all monitors that it has locked (a natural consequence of unchecked Threaddeath exceptions propagated up the stack). If any object previously protected by these monitors is in an inconsistent state, the corrupted object will be visible to other threads, which can result in arbitrary behavior. This behavior can be subtle, imperceptible, and possibly significant. Unlike other unchecked exceptions, the Threaddeath exception kills the thread in the background, so the user does not get a warning that his program may be corrupted. This damage is likely to manifest at any time after the actual damage occurs, and possibly in hours or even in the next few days. It is also mentioned in the document that programmers cannot fix corrupted objects by capturing Threaddeath exceptions. See the original for specific reasons. Since the Stop method is not recommended, what method should be used to proxy stop to implement the corresponding function? 1, by modifying the shared variable to notify the target thread to stop running most of the places that need to use the stop should use this method to achieve the purpose of the interrupt thread. This approach has several requirements or considerations: (1) The target thread must check the variable regularly, and when the variable indicates that it should stop running, the thread should return in a certain order from the method it executes. (2) The variable must be defined as volatile, or all accesses to it must be synchronized (synchronized). For example: If your applet includes Start,stop,run several methods:
private thread blinker; Public void start () { blinker = new thread (This); blinker.start (); } Public void stop () { blinker.stop (); // unsafe! } Public void run () { Thread Thisthread = thread.currentthread (); while (True) { try { thisthread.sleep (interval); } catch ( interruptedexception e) { } repaint (); } } You can avoid using the Thread.stop method in the following ways:
private volatile thread blinker; Public void stop () { blinker = null; } Public void run () { thread thisthread = thread.currentthread (); while (blinker == thisthread) { try { thisthread.sleep (interval ); } catch (interruptedexception e) { } repaint (); } } 2, broken threads through the Thread.Interrupt method Normally, We should use the first approach instead of the Thread.stop method. However, there are several ways in which you should use the Thread.Interrupt method to break threads (this method is usually used in conjunction with the first method). When you start using the interrupt method, there is a strange feeling that there is something wrong with the method. The API document says that the partymethod is used for "interrupts this thread". Take a look at the following example:
Package com.polaris.thread; public class Testthread implements runnable{Boolean stop = false; public static void Main (string[] args) throws Exception {thread thread = new Thread (new Testthread (), ' My Thread ' ); SYSTEM.OUT.PRINTLN ("Starting thread ..."); <