注意:我们说中断或者说停止一个运行的线程,是指需要在实际编写代码中就写好了相关操作。
一、需要进行线程中断的场景
- 很多线程的运行模式是死循环,比如在生产者/消费者模式中,消费者主体就是一个死循环,它不停的从队列中接受任务,执行任务,在停止程序时,我们需要一种"优雅"的方法以关闭该线程。
- 在一些用户启动的任务中,线程是用户启动的,比如手动启动批次任务,在任务执行过程中,用户可能会希望取消该任务。
- 在一些场景中,比如从第三方服务器查询一个结果,我们希望在限定的时间内得到结果,如果得不到,我们会希望取消该任务。
- 有时,我们会启动多个线程做同一件事,比如类似抢火车票,我们可能会让多个好友帮忙从多个渠道买火车票,只要有一个渠道买到了,我们会通知取消其他渠道。
二、中断线程的三种方法
一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止,自己来决定自己的命运。所以Thread.stop(),Thread.supend(),Thread.resume()这些方法都已经被废弃了。
因此,我们要中断一个线程,可以采取如下三种方法:
方法一:通过volatile关键字
通过volatile标识一个boolean类型的属性,线程中判断该属性的状态来控制线程的中断与否。
public class Test {
static volatile boolean isStop = false;
public static void main(String[] args) {
new Thread(()->{
while (true){
if (isStop){
System.out.println("当前线程中断执行");
break;
}
System.out.println("------ hello");
}
},"t1").start();
try {Thread.sleep(200);} catch (InterruptedException e) {throw new RuntimeException(e);}
new Thread(()->{
isStop = true;
},"t2").start();
}
}
方法二:通过AtomicBoolean对象
作用原理都是一样,设置一个判断条件,当满足条件时退出该条件判断。
import java.util.concurrent.atomic.AtomicBoolean;
public class Test {
// static volatile boolean isStop = false
static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
public static void main(String[] args) {
new Thread(()->{
while (true){
if (atomicBoolean.get()){
System.out.println("当前线程中断执行");
break;
}
System.out.println("------ hello");
}
},"t1").start();
try {Thread.sleep(200);} catch (InterruptedException e) {throw new RuntimeException(e);}
new Thread(()->{
atomicBoolean.set(true);
},"t2").start();
}
}
方法三:通过Thread类的interrupt()和isInterrupted()方法
其实,interrupt()方法也只是将线程的中断状态设置为true,并不会中断线程。实际的中断线程,通过上述方法案例也知道,就是让这个线程跳出循环执行完就可以了。isInterruped()方法则是判断这个线程是否被中断。
另外注意:如果线程已经结束了,此时再通过isInterrupted()方法判断显示为false,是因为中断不活动的线程是不会产生影响的。 它是自己结束的(或者根本没启动),并不是被中断的。
public class Test {
// static volatile boolean isStop = false
// static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("当前线程中断执行");
break;
}
System.out.println("------ hello");
}
}, "t1");
t1.start();
try {Thread.sleep(200);} catch (InterruptedException e) {throw new RuntimeException(e);}
new Thread(()->{
t1.interrupt();
},"t2").start();
}
}
需要注意的是:
interrupt()方法,如果该线程阻塞的调用wait() , wait(long) ,或wait(long, int)的方法Object类,或者在join() , join(long) , join(long, int) , sleep(long) ,或sleep(long, int) ,这个类的方法,那么它的中断状态将被清除,并且将收到一个InterruptedException 。
package com.hssy.jucstudy.juc.interrupt;
public class InterruptTest {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true){
if (Thread.currentThread().isInterrupted()){
System.out.println("当前线程中断执行");
break;
}
System.out.println("------ hello");
try {Thread.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}
}
}, "t1");
t1.start();
// 让t1线程先执行一会儿,然后其他线程再给它设置中断状态
try {Thread.sleep(200);} catch (InterruptedException e) {e.printStackTrace();}
new Thread(()->{
t1.interrupt();
},"t2").start();
}
}
关于静态方法interrupted()
public static boolean interrupted()
测试当前线程是否中断。 该方法可以清除线程的中断状态 。 换句话说,如果这个方法被连续调用两次,那么第二个调用将返回false(除非当前线程再次中断,在第一个调用已经清除其中断状态之后,在第二个调用之前已经检查过)。
忽略线程中断,因为线程在中断时不存在将被该方法返回false所反映。结果 true如果当前线程已被中断; false否则。