java isinterrupted_JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())

一,介绍

本文记录JAVA多线程中的中断机制的一些知识点。主要是stop方法、interrupted()与isInterrupted()方法的区别,并从源代码的实现上进行简单分析。

JAVA中有3种方式可以终止正在运行的线程

①线程正常退出,即run()方法执行完毕了

②使用Thread类中的stop()方法强行终止线程。但stop()方法已经过期了,不推荐使用

③使用中断机制

线程正常退出没有什么东东,中断机制下面详细介绍,先看下stop()方法的源代码,关键是源代码上的注释。它解释了为什么stop()不安全,stop()方法停止的是哪个线程?

1 /**

2 * Forces the thread to stop executing.3 *

4 * If there is a security manager installed, its checkAccess5 * method is called with this6 * as its argument. This may result in a7 * SecurityException being raised (in the current thread).8 *

9 * If this thread is different from the current thread (that is, the current10 * thread is trying to stop a thread other than itself), the11 * security manager's checkPermission method (with a12 * RuntimePermission("stopThread") argument) is called in13 * addition.14 * Again, this may result in throwing a15 * SecurityException (in the current thread).16 *

17 * The thread represented by this thread is forced to stop whatever18 * it is doing abnormally and to throw a newly created19 * ThreadDeath object as an exception.20 *

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 *

24 * An application should not normally try to catch25 * ThreadDeath unless it must do some extraordinary26 * cleanup operation (note that the throwing of27 * ThreadDeath causes finally clauses of28 * try statements to be executed before the thread29 * officially dies). If a catch clause catches a30 * ThreadDeath object, it is important to rethrow the31 * object so that the thread actually dies.32 *

33 * The top-level error handler that reacts to otherwise uncaught34 * exceptions does not print out a message or otherwise notify the35 * application if the uncaught exception is an instance of36 * ThreadDeath.37 *38 *@exceptionSecurityException if the current thread cannot39 * modify this thread.40 *@see#interrupt()41 *@see#checkAccess()42 *@see#run()43 *@see#start()44 *@seeThreadDeath45 *@seeThreadGroup#uncaughtException(Thread,Throwable)46 *@seeSecurityManager#checkAccess(Thread)47 *@seeSecurityManager#checkPermission48 *@deprecatedThis method is inherently unsafe. Stopping a thread with49 * Thread.stop causes it to unlock all of the monitors that it50 * has locked (as a natural consequence of the unchecked51 * ThreadDeath exception propagating up the stack). If52 * any of the objects previously protected by these monitors were in53 * an inconsistent state, the damaged objects become visible to54 * other threads, potentially resulting in arbitrary behavior. Many55 * uses of stop should be replaced by code that simply56 * modifies some variable to indicate that the target thread should57 * stop running. The target thread should check this variable58 * regularly, and return from its run method in an orderly fashion59 * if the variable indicates that it is to stop running. If the60 * target thread waits for long periods (on a condition variable,61 * for example), the interrupt method should be used to62 * interrupt the wait.63 * For more information, see64 * Why65 * are Thread.stop, Thread.suspend and Thread.resume Deprecated?.66 */

67 @Deprecated68 public final voidstop() {69 stop(newThreadDeath());70 }

上面注释,第9行到第16行表明,stop()方法可以停止“其他线程”。执行thread.stop()方法这条语句的线程称为当前线程,而“其他线程”则是 调用thread.stop()方法的对象thread所代表的线程。

如:

1 public static voidmain(String[] args) {2 MyThread thread = newMyThread...3 //.....

4 thread.stop();5 //....

6 }

在main方法中,当前线程就是main线程。它执行到第4行,想把“其他线程”thread“ 给停止。这个其他线程就是MyThread类 new 的thread对象所表示的线程。

第21行至23行表明,可以停止一个尚未started(启动)的线程。它的效果是:当该线程启动后,就立马结束了。

第48行以后的注释,则深刻表明了为什么stop()方法被弃用!为什么它是不安全的。

比如说,threadA线程拥有了监视器,这些监视器负责保护某些临界资源,比如说银行的转账的金额。当正在转账过程中,main线程调用 threadA.stop()方法。结果导致监视器被释放,其保护的资源(转账金额)很可能出现不一致性。比如,A账户减少了100,而B账户却没有增加100

二,中断机制

JAVA中如何正确地使用中断机制的细节太多了。interrupted()方法与 isInterrupted()方法都是反映当前线程的是否处于中断状态的。

①interrupted()

1 /**

2 * Tests whether the current thread has been interrupted. The3 * interrupted status of the thread is cleared by this method. In4 * other words, if this method were to be called twice in succession, the5 * second call would return false (unless the current thread were6 * interrupted again, after the first call had cleared its interrupted7 * status and before the second call had examined it).8 *9 *

A thread interruption ignored because a thread was not alive10 * at the time of the interrupt will be reflected by this method11 * returning false.12 *13 *@returntrue if the current thread has been interrupted;14 * false otherwise.15 *@see#isInterrupted()16 * @revised 6.017 */

18 public static booleaninterrupted() {19 return currentThread().isInterrupted(true);20 }

从源码的注释中看出,它测试的是当前线程(current thread)的中断状态,且这个方法会清除中断状态。

②isInterrupted()

1 /**

2 * Tests whether this thread has been interrupted. The interrupted3 * status of the thread is unaffected by this method.4 *5 *

A thread interruption ignored because a thread was not alive6 * at the time of the interrupt will be reflected by this method7 * returning false.8 *9 *@returntrue if this thread has been interrupted;10 * false otherwise.11 *@see#interrupted()12 * @revised 6.013 */

14 public booleanisInterrupted() {15 return isInterrupted(false);16 }

从源码注释中可以看出,isInterrupted()方法不会清除中断状态。

③interrupted()方法与 isInterrupted()方法的区别

从源代码可以看出,这两个方法都是调用的isInterrupted(boolean ClearInterrupted),只不过一个带的参数是true,另一个带的参数是false。

1 /**

2 * Tests if some Thread has been interrupted. The interrupted state3 * is reset or not based on the value of ClearInterrupted that is4 * passed.5 */

6 private native boolean isInterrupted(boolean ClearInterrupted);

因此,第一个区别就是,一个会清除中断标识位,另一个不会清除中断标识位。

再分析源码,就可以看出第二个区别在return 语句上:

public static booleaninterrupted() {return currentThread().isInterrupted(true);

}/************************/

public booleanisInterrupted() {return isInterrupted(false);

}

interrupted()测试的是当前的线程的中断状态。而isInterrupted()测试的是调用该方法的对象所表示的线程。一个是静态方法(它测试的是当前线程的中断状态),一个是实例方法(它测试的是实例对象所表示的线程的中断状态)。

下面用个具体的例子来更进一步地阐明这个区别。

有一个自定义的线程类如下:

1 public class MyThread extendsThread {2 @Override3 public voidrun() {4 super.run();5 for (int i = 0; i < 500000; i++) {6 System.out.println("i=" + (i + 1));7 }8 }9 }

先看interrupted()方法的示例:

1 public classRun {2 public static voidmain(String[] args) {3 try{4 MyThread thread = newMyThread();5 thread.start();6 Thread.sleep(1000);7 thread.interrupt();8 //Thread.currentThread().interrupt();

9 System.out.println("是否停止1?="+thread.interrupted());//false10 System.out.println("是否停止2?="+thread.interrupted());//false main线程没有被中断!!!

//......

第5行启动thread线程,第6行使main线程睡眠1秒钟从而使得thread线程有机会获得CPU执行。

main线程睡眠1s钟后,恢复执行到第7行,请求中断 thread线程。

第9行测试线程是否处于中断状态,这里测试的是哪个线程呢???答案是main线程。因为:

(1)interrupted()测试的是当前的线程的中断状态

(2)main线程执行了第9行语句,故main线程是当前线程

再看isInterrupted()方法的示例:

1 public classRun3 {2 public static voidmain(String[] args) {3 try{4 MyThread thread = newMyThread();5 thread.start();6 Thread.sleep(1000);7 thread.interrupt();8 System.out.println("是否停止1?="+thread.isInterrupted());//true

在第8行,是thread对象调用的isInterrupted()方法。因此,测试的是thread对象所代表的线程的中断状态。由于在第7行,main线程请求中断 thread线程,故在第8行的结果为: true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值