Java中的线程中断是一种线程间的协作模式,通过设置线程的中断标志并不能直接终止该线程的执行,而是被中断的线程根据中断状态自行处理。
就是说:我告诉你要中断,不一定要立马中断,什么时候中断要看你自己的执行情况自己中断,最终还是要中断的。
● void interrupt()方法 :中断线程,例如,当线程A运行时,线程B可以调用线程A的interrupt()
方法来设置线程A的中断标志为true并立即返回。中断标志是线程类的一个属性。
Thread类部分源码
private volatile boolean interrupted;
设置标志仅仅是设置标志,并不是立即终止线程的执行,而是给线程发送一个中断信号。具体如何响应中断信号需要由线程的代码逻辑进行处理。通常情况下,在线程的代码中需要使用isInterrupted()方法来检查线程的中断状态,并根据中断状态执行相应的处理逻辑。最终还是会中断。
如果线程A因为调用了wait系列函数、join方法或者sleep方法而被阻塞挂起,这时候若线程B调用线程A的interrupt()方法,线程A会在调用这些方法的地方抛出InterruptedException异常而返回。
● boolean isInterrupted()方法:检测当前线程是否被中断,如果是返回true,否则返回false。
public boolean isInterrupted() {
//传递false,说明不清除中断标志
return isInterrupted(false);
}
● boolean interrupted()方法:检测当前线程是否被中断,并不是调用者,如果是返回true,否则返回false。与isInterrupted不同的是,该方法如果发现当前线程被中断,则会清除中断标志,并且该方法是static方法,可以通过Thread类直接调用。另外从下面的代码可以知道,在interrupted()内部是获取当前调用线程的中断标志而不是调用interrupted()方法的实例对象的中断标志。
public static boolean interrupted() {
//清除中断标志
return currentThread().isInterrupted(true);
}
一个线程使用Interrupted优雅退出的经典例子,代码如下
public void run(){
try{
....
//线程退出条件
while(! Thread.currentThread().isInterrupted()&& more work to do){
// do more work;
}
}catch(InterruptedException e){
// thread was interrupted during sleep or wait
}
finally{
// cleanup, if required
}
}
下面看一个根据中断标志判断线程是否终止的例子
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//如果当前线程被中断则退出循环
while (! Thread.currentThread().isInterrupted())
System.out.println(Thread.currentThread() + " hello");
}
});
//启动子线程
thread.start();
//主线程休眠1s,以便中断前让子线程输出
Thread.sleep(1000);
//中断子线程
System.out.println("main thread interrupt thread");
//设置线程A的中断标志为true并立即返回,仅仅是设置标志,线程A实际并没有被中断,它会继续往下执行。
thread.interrupt();
//主线程被阻塞也就是当前main方法,等待子线程执行完毕
thread.join();
System.out.println("main is over");
}
执行结果:
如上代码中,threadOne线程休眠了2000s,在正常情况下该线程需要等到2000s后才会被唤醒,但是本例通过调用threadOne.interrupt()方法打断了该线程的休眠,该线程会在调用sleep方法处抛出InterruptedException异常后返回。
再通过一个例子来了解interrupted()与isInterrupted()方法的不同之处。
public static void main(String[] args) throws InterruptedException {
Thread threadOne = new Thread(new Runnable() {
public void run() {
for(; ; ){
}
}
});
//启动线程
threadOne.start();
//只是设置中断标志,但没有真的中断
threadOne.interrupt();
//获取中断标志
System.out.println("isInterrupted:" + threadOne.isInterrupted());//检测threadOne线程是否中断
//获取中断标志并重置
System.out.println("isInterrupted:" + threadOne.interrupted());//检测当前线程是否中断,中断清除中断标志
//获取中断标志并重置
System.out.println("isInterrupted:" + Thread.interrupted());//Thread是指main方法当前线程
//获取中断标志
System.out.println("isInterrupted:" + threadOne.isInterrupted());
threadOne.join();
System.out.println("main thread is over");
}
执行结果
上面我们介绍了在
interrupted()
方法内部是获取当前线程的中断状态,这里虽然调用了threadOne的interrupted()
方法,但是获取的是主线程的中断标志,因为主线程是当前线程。threadOne.interrupted()
和Thread.interrupted()
方法的作用是一样的,目的都是获取当前线程的中断标志。
public static void main(String[] args) throws InterruptedException {
Thread threadOne = new Thread(new Runnable() {
public void run() {
//中断标志为true时会退出循环,并且清除中断标志
while (! Thread.currentThread().interrupted()) {
}
System.out.println("threadOne isInterrupted:" + Thread.currentThread().
isInterrupted());
}
});
// 启动线程
threadOne.start();
// 设置中断标志
threadOne.interrupt();
threadOne.join();
System.out.println("main thread is over");
}
由输出结果可知,调用interrupted()方法后中断标志被清除了