interrupt、interrupted 、isInterrupted 区别
分类:
多线程(52)
1、interrupt
interrupt 方法用于中断线程。调用该方法的线程的状态为将被置为"中断"状态。
注意:线程中断仅仅是置线程的中断状态位,不会停止线程。需要用户自己去监
视线程的状态为并做处理。支持线程中断的方法(也就是线程中断后会抛出
interruptedException 的方法)就是在监视线程的中断状态,一旦线程的中断
状态被置为“中断状态”,就会抛出中断异常。
一般来说,支持线程中断的方法(也就是线程中断后会抛出 InterruptedException 的方
法),阻塞函数,如:Thread.sleep、Thread.join、Object.wait 等在检查到线程的中断
状态时,会抛出 InterruptedException,同时会清除线程的中断状态
2、interrupted 和 isInterrupted
首先看一下该方法的实现:
public static boolean interrupted
() {
return currentThread().isInterrupted(true);
该方法就是直接调用当前线程的 isInterrupted(true)方法。
然后再来看一下 isInterrupted 的实现:
public boolean isInterrupted
() {
return isInterrupted( false);
评论0