1. 什么是中断?
1. 一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止。所以,Thread stop, Thread. suspend, Thread.resume都已经被废弃了。
2. 在Java中没有办法立即停止一条线程,然而停止线程却显得尤为重要,如取消一个耗时操作。因此,Java提供了一种用于停止线程的机制————中断。
3. 中断只是一种协作机制(并不能立刻stop一个线程),Java没有给中断增加任何语法,中断的过程完全需要程序员自己实现。若要中断一个线程,你需要手动调用该线程的interrupt方法,该方法也仅仅是将线程对象的中断标识设成true; 接着你需要自己写代码不断地检测当前线程的标识位,如果为true,表示别的线程要求这条线程中断,此时究竟该做什么需要你自己写代码实
4. 每个线程对象中都有一个标识,用于表示线程是否被中断;该标识位为true表示中断,为false表示未中断。通过调用线程对象的interrupt方法将该线程的标识位设为true;可以在别的线程中调用,也可以在自己的线程中调用。
2. 使用中断标识停止线程的的3种方法
1. 通过一个volatile变量实现
2. 通过AtomicBoolean
3. 通过Thread类自带的中断api方法实现
代码示例
1. 通过一个volatile变量实现
通过volatile设置变量isStop,当isStop = true时,break中断线程。(volatile具有线程间的可见性)
public class InterruptDemo01 {
//通过一个volatile变量实现
//volatile具有可见性
static volatile boolean isStop = false;
public static void main(String[] args) {
new Thread(()->{
while (true){
if(isStop){
System.out.println("-------isStop = true, 程序结束");
break;
}
System.out.println("-----hello");
}
},"t1").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
isStop = true;
},"t2").start();
}
}
2. 通过AtomicBoolean
当atomicBoolean.get() = true时,break中断线程
public class InterruptDemo02 {
//通过AtomicBoolean
static AtomicBoolean atomicBoolean = new AtomicBoolean(false);
public static void main(String[] args) {
new Thread(()->{
while (true){
if(atomicBoolean.get()){
System.out.println("-------atomicBoolean.get() = true, 程序结束");
break;
}
System.out.println("-----hello");
}
},"t1").start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()->{
atomicBoolean.set(true);
},"t2").start();
}
}
3. 通过Thread类自带的中断api方法实现
实例方法, interrupt()仅仅是设置线程的中断状态为true,不会停止线程
实例方法,isInterrupt()判断当前线程是否被中断(通过检查中断标志位)
public class InterruptDemo03 {
//通过通过Thread类自带的中断api方法实现
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("-------isInterrupted() = true, 程序结束");
break;
}
System.out.println("-----hello");
}
});
t1.start();
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}
3. 当中断标志位被设置为true时,线程是否就被中断? 否
具体来说,当对一个线程,调用interrupt()时:
1. 如果线程处于正常活动状态,那么会将该线程的中断标志设置为true. 仅此而已。被设置中断标志的线程将继续正常运行,不受影响。所以,interrupt() 并不能真正的中断线程,需要被调用的线程自己进行配合才行。
2. 如果线程处于被阻塞状态(例如处于sleep, wait, join等状态),在别的线程中调用当前线程对象的interrupt方法,那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常。
代码示例
线程处于正常活动状态
t1.interrupt()被调用之前,t1的中断标志位默认值为false; 主线程暂停3毫秒后,此时调用t1.interrupt(),将t1的中断标识设置为true,但t1并没有停止;主线程再次暂停5秒,此时t1线程已经结束执行,t1的中断标识为false。(t1.interrupt()被调用02时为true,因线程还未完全结束)
public class InterruptDemo04 {
//中断标志位为true后,并不是立刻stop程序
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
for (int i = 0; i < 300; i++) {
System.out.println("i:" + i);
}
System.out.println("t1.interrupt()被调用02:"+Thread.currentThread().isInterrupted());//true
});
t1.start();
System.out.println("t1.interrupt()被调用之前,t1的中断标志位默认值:"+t1.isInterrupted());//false
TimeUnit.MICROSECONDS.sleep(3);
t1.interrupt();//将t1的中断标志位为true,t1并没有停
System.out.println("t1.interrupt()被调用01:"+t1.isInterrupted());//true
TimeUnit.MICROSECONDS.sleep(5000);
//5秒后,非活跃状态,t1线程已经结束执行
System.out.println("t1.interrupt()被调用03:"+t1.isInterrupted());//false
}
}
线程处于被阻塞状态
线程每隔0.5秒休眠一次(处于sleep状态),此时被interrupt,下列程序将如何?
线程将无法停止
public class InterruptDemo05 {
//线程处于被阻塞状态时,被interrupt后,抛出一个InterruptedException异常,且中断标志位被复位为false
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("-------isInterrupted() = true, 程序结束");
break;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-----hello");
}
});
t1.start();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}
原因:sleep方法抛出InterruptedException后,中断标识也被清空置为false,我们在catch没有通过调用interrupt() 方法再次将中断标识置为true, 这就导致无限循环了
修正:在catch中再次调用interrupt()
public class InterruptDemo05 {
//线程处于被阻塞状态时,被interrupt后,抛出一个InterruptedException异常,且中断标志位被复位为false
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("-------isInterrupted() = true, 程序结束");
break;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
//解决出现异常的bug
Thread.currentThread().interrupt();
e.printStackTrace();
}
System.out.println("-----hello");
}
});
t1.start();
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
t1.interrupt();
}
}
4. 静态方法 interrupted()
实例方法, interrupt()仅仅是设置线程的中断状态为true,不会停止线程
实例方法,isInterrupt()判断当前线程是否被中断(通过检查中断标志位)
静态方法,interrupted()判断线程是否被中断,并清除当前中断状态: 1 返回当前线程的中断状态 2 将当前线程的中断状态设为false
区别:
静态方法interrupted将会清除中断状态 (传入的参数ClearInterrupted为true)
实例方法isInterrupted则不会(传入的参数ClearInterrupted为false)
代码示例
线程中断标识默认为false,执行interrupt()后,线程中断标识为true,但是interrupted将会清除中断状态,并将当前中断状态设为false,故最后线程的中断标识为false
public class InterruptDemo06 {
//静态方法,Thread.interrupted() 1返回当前线程的中断状态 2将当前线程的中断状态设为false
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-----"+Thread.interrupted());//false
System.out.println(Thread.currentThread().getName()+"-----"+Thread.interrupted());//false
System.out.println("-----------------");
Thread.currentThread().interrupt();//false->true
System.out.println("-----------------");
System.out.println(Thread.currentThread().getName()+"-----"+Thread.interrupted());//true->false
System.out.println(Thread.currentThread().getName()+"-----"+Thread.interrupted());//false
}
}