Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出 阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join,Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。
建议使用外部的布尔变量进行控制,比如:
class MyThread extends Thread { volatile boolean finished = false; public void stopMe() { finished = true; } public void run() { while (!finished) { //do dirty work } } }
public class AlternateStop implements Runnable { private volatile boolean stopRequested; private Thread runThread; public void run() { runThread = Thread.currentThread(); stopRequested = false; int count = 0; while (!stopRequested) { System.out.println("Running ... count=" + count); count++; try { Thread.sleep(300); } catch (InterruptedException x) { System.out.println("捕获异常后---"+Thread.currentThread().isInterrupted()); Thread.currentThread().interrupt(); // re-assert interrupt System.out.println("重新设置打断状态---"+Thread.currentThread().isInterrupted()); } } System.out.println("stoped"); } public void stopRequest() { stopRequested = true; if (runThread != null) { runThread.interrupt(); } } public static void main(String[] args) { AlternateStop as = new AlternateStop(); Thread t = new Thread(as); t.start(); try { Thread.sleep(2000); } catch (InterruptedException x) { // ignore } as.stopRequest(); } }
注意当一个阻塞方法检测到中断并抛出 InterruptedException 时,它清除中断状态,为了让上层代码知道 该线程的状态,重新设置打断状态。
System.out.println("捕获异常后---"+Thread.currentThread().isInterrupted()); Thread.currentThread().interrupt(); // re-assert interrupt System.out.println("重新设置打断状态---"+Thread.currentThread().isInterrupted());
以上三行代码会打印:
捕获异常后---false
重新设置打断状态---true
相关推荐
│ 高并发编程第一阶段15讲、Thread中断Interrupt方法详细讲解.mp4 │ 高并发编程第一阶段16讲、采用优雅的方式结束线程生命周期.mp4 │ 高并发编程第一阶段17讲、Thread API综合实战,编写ThreadService实现暴力...
1.3.3 使用中断(Interrupt)取消线程........................................................................15 1.3.4 使用Stop 终止线程........................................................................
│ 高并发编程第一阶段15讲、Thread中断Interrupt方法详细讲解.mp4 │ 高并发编程第一阶段16讲、采用优雅的方式结束线程生命周期.mp4 │ 高并发编程第一阶段17讲、Thread API综合实战,编写ThreadService实现暴力...
线程中断规则:对线程interrupt()方法的调用先行发生于被中断线程的代码检测到中断事件的发生; g.线程终结规则:线程中所有的操作都先行发生于线程的终止检测,我们可以通过Thread.join()方法结束、Thread.isAlive...
1.3.3 使用中断(Interrupt)取消线程........................................................................15 1.3.4 使用Stop 终止线程........................................................................
线程 ...................................................................................................................................................... 20 2.2. JVM 内存区域 .........................
Java 中的线程中断是一种线程间的协作模式,通过设置线程的中断标志并不能直接终止该线程的执行,而是被中断的线程根据中断状态自行处理。即“线程中断”并不是字面意思——线程真的中断了,而是设置了中断标志...
2.1. 线程 ...................................................................................................................................................... 20 2.2. JVM 内存区域 .....................
interrupt 方法:中断线程的执行。 synchronized 关键字:用于实现线程的同步,确保多个线程之间的安全访问共享资源。 Lock 接口和 ReentrantLock 类:提供更灵活的线程同步机制。 Executor 框架和线程池:用于管理...
线程安全,并发的知识有加深认知;当然,现在用过的东西并不是代表以后还能娴熟的使用,做好笔记非常重要; 1:必须明白为什么要使用线程池:(这点很重要) a:手上项目所需,因为项目主要的目的是实现多线程的...