本文记录Java多线程中的中断机制的一些知识点。主要是stop方法、interrupted()与isInterrupted()方法的区别,并从源代码的实现上进行简单分析。
Java中有3种方式可以终止正在运行的线程
①线程正常退出,即run()方法执行完毕了
②使用Thread类中的stop()方法强行终止线程。但stop()方法已经过期了,不推荐使用
③使用中断机制
线程正常退出没有什么东东,中断机制下面详细介绍,先看下stop()方法的源代码,关键是源代码上的注释。它解释了为什么stop()不安全,stop()方法停止的是哪个线程?
1 /** 2 * Forces the thread to stop executing. 3 * <p> 4 * If there is a security manager installed, its <code>checkAccess</code> 5 * method is called with <code>this</code> 6 * as its argument. This may result in a 7 * <code>SecurityException</code> being raised (in the current thread). 8 * <p> 9 * If this thread is different from the current thread (that is, the current 10 * thread is trying to stop a thread other than itself), the 11 * security manager's <code>checkPermission</code> method (with a 12 * <code>RuntimePermission("stopThread")</code> argument) is called in 13 * addition. 14 * Again, this may result in throwing a 15 * <code>SecurityException</code> (in the current thread). 16 * <p> 17 * The thread represented by this thread is forced to stop whatever 18 * it is doing abnormally and to throw a newly created 19 * <code>ThreadDeath</code> object as an exception. 20 * <p> 21 * It is permitted to stop a thread that has not yet been started. 22 * If the thread is eventually started, it immediately terminates. 23 * <p> 24 * An application should not normally try to catch 25 * <code>ThreadDeath</code> unless it must do some extraordinary 26 * cleanup operation (note that the throwing of 27 * <code>ThreadDeath</code> causes <code>finally</code> clauses of 28 * <code>try</code> statements to be executed before the thread 29 * officially dies). If a <code>catch</code> clause catches a 30 * <code>ThreadDeath</code> object, it is important to rethrow the 31 * object so that the thread actually dies. 32 * <p> 33 * The top-level error handler that reacts to otherwise uncaught 34 * exceptions does not print out a message or otherwise notify the 35 * application if the uncaught exception is an instance of 36 * <code>ThreadDeath</code>. 37 * 38 * @exception SecurityException if the current thread cannot 39 * modify this thread. 40 * @see #interrupt() 41 * @see #checkAccess() 42 * @see #run() 43 * @see #start() 44 * @see ThreadDeath 45 * @see ThreadGroup#uncaughtException(Thread,Throwable) 46 * @see SecurityManager#checkAccess(Thread) 47 * @see SecurityManager#checkPermission 48 * @deprecated This method is inherently unsafe. Stopping a thread with 49 * Thread.stop causes it to unlock all of the monitors that it 50 * has locked (as a natural consequence of the unchecked 51 * <code>ThreadDeath</code> exception propagating up the stack). If 52 * any of the objects previously protected by these monitors were in 53 * an inconsistent state, the damaged objects become visible to 54 * other threads, potentially resulting in arbitrary behavior. Many 55 * uses of <code>stop</code> should be replaced by code that simply 56 * modifies some variable to indicate that the target thread should 57 * stop running. The target thread should check this variable 58 * regularly, and return from its run method in an orderly fashion 59 * if the variable indicates that it is to stop running. If the 60 * target thread waits for long periods (on a condition variable, 61 * for example), the <code>interrupt</code> method should be used to 62 * interrupt the wait. 63 * For more information, see 64 * <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why 65 * are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>. 66 */ 67 @Deprecated 68 public final void stop() { 69 stop(new ThreadDeath()); 70 }