线程中断interrupt


转链接:https://www.zhihu.com/question/41048032/answer/89431513


一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止。
所以,Thread.stop, Thread.suspend, Thread.resume 都已经被废弃了。
而 Thread.interrupt 的作用其实也不是中断线程,而是「通知线程应该中断了」,
具体到底中断还是继续运行,应该由被通知的线程自己处理。

具体来说,当对一个线程,调用 interrupt() 时,
① 如果线程处于被阻塞状态(例如处于sleep, wait, join 等状态),那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常。仅此而已。
② 如果线程处于正常活动状态,那么会将该线程的中断标志设置为 true,仅此而已。被设置中断标志的线程将继续正常运行,不受影响。

interrupt() 并不能真正的中断线程,需要被调用的线程自己进行配合才行。
也就是说,一个线程如果有被中断的需求,那么就可以这样做。
① 在正常运行任务时,经常检查本线程的中断标志位,如果被设置了中断标志就自行停止线程。
② 在调用阻塞方法时正确处理InterruptedException异常。(例如,catch异常后就结束线程。)
Thread thread = new Thread(() -> {
    while (!Thread.interrupted()) {
        // 清除标志位,继续执行线程
    }
});
thread.start();

// 一段时间以后
thread.interrupt();

具体到你的问题,Thread.interrupted()清除标志位是为了下次继续检测标志位。
如果一个线程被设置中断标志后,选择结束线程那么自然不存在下次的问题,
而如果一个线程被设置中断标识后,进行了一些处理后选择继续进行任务,
而且这个任务也是需要被中断的,那么当然需要清除标志位了。

interrupt()和isInterrupted()配合结束线程:在判断外的语句,即使是中断还会执行,所以应该把中断和正常的代码分成两块写在if判断逻辑中

public class TestThread implements Runnable {

	@Override
	public void run() {
		
		if(!Thread.currentThread().isInterrupted()){
			System.out.println("正常");
		}
				
		if(Thread.currentThread().isInterrupted()){
			System.out.println("线程中断的处理代码");
		}
		System.out.println("这句话一定会执行,哪怕是中断了线程");
	}

	public static void main(String[] args) {
		Thread t=new Thread(new TestThread());
		t.start();
		t.interrupt();
		System.out.println("主线程");
	}
}

interrupted()清除标志位:

public class TestThread implements Runnable {

	@Override
	public void run() {
		if(Thread.currentThread().interrupted()){
			System.out.println("清除标志位,继续执行线程");
		}
		if(!Thread.currentThread().isInterrupted()){
			System.out.println("正常");
		}
				
		if(Thread.currentThread().isInterrupted()){
			System.out.println("线程中断的处理代码");
		}
		System.out.println("这句话一定会执行,哪怕是中断了线程");
	}

	public static void main(String[] args) {
		Thread t=new Thread(new TestThread());
		t.start();
		t.interrupt();
		System.out.println("主线程");
	}
}


注意:interrupt()和isInterrupted() 执行对象是任何线程thread对象,而interrupted()是静态方法,执行对象就是当前线程

猜你喜欢

转载自blog.csdn.net/u014203449/article/details/80215596