1. 线程中断的含义
当我们创建一个线程循环打印语句时,我们想要发出一个指令让该线程停下来,此时,我们可以发送线程中断的命令
线程是否中断实际上是根据一个表示是否中断的标志位来决定的,这个标志位我们既可以自定义,也可以使用Thread内部的一个boolean变量来表示
2. 自定义标志位
class MyRunnable implements Runnable {
public volatile boolean isQuit = false;
@Override
public void run() {
while (isQuit == false) {
System.out.println("isQuit is false");
}
System.out.println("isQuit is true");
}
}
public class Demo15 {
public static void main(String[] args) throws InterruptedException {
MyRunnable myRunnable = new MyRunnable();
Thread t = new Thread(myRunnable);
t.start();
Thread.sleep(100);
myRunnable.isQuit = true;
}
}
3. 使用interrupt()方法
一、interrupt()中断对象关联的线程,如果线程正在阻塞,则以异常方式通知,否则设置标志位
public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
while (true) {
System.out.println("hello");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().isInterrupted());
e.printStackTrace();
System.out.println("thread is interrupted");
break;
}
}
});
t.start();
Thread.sleep(3000);
t.interrupt();
}
}
二、Thread.interrupted()判断当前线程的中断标志位是否设置,调用后清除标志位
public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
while (!Thread.interrupted()) {
System.out.println("hello");
}
System.out.println(Thread.interrupted());
});
t.start();
Thread.sleep(100);
t.interrupt();
}
}
三、Thread.currentThread().isInterrupted()判断对象关联的线程的标志位是否设置,调用后不清除标志位
public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(()->{
while (!Thread.currentThread().isInterrupted()) {
System.out.println("hello");
}
System.out.println(Thread.currentThread().isInterrupted());
});
t.start();
Thread.sleep(100);
t.interrupt();
}
}
故线程收到通知的方式有两种:
- 如果线程因为调用 wait/join/sleep 等方法而阻塞挂起,则以InterruptedException 异常的形式通知,清除中断标志
- 当出现 InterruptedException 的时候, 要不要结束线程取决于 catch 中代码的写法. 可以选择忽略这个异常, 也可以跳出循环结束线程.
- 否则,只是内部的一个中断标志被设置,thread 可以通过
Thread.interrupted() 判断当前线程的中断标志被设置,清除中断标志
Thread.currentThread().isInterrupted() 判断指定线程的中断标志被设置,不清除中断标志