一、源码阅读
1 interrupt()
Thread#interrupt()的作用其实也不是中断线程,而是通知线程应该中断了,具体来说,当对一个线程调用interrupt() 时:
- 如果线程处于被阻塞状态(例如处于sleep, wait, join 等状态),那么线程将立即退出被阻塞状态,并抛出一个InterruptedException异常,仅此而已。
- 如果线程处于正常活动状态,那么会将该线程的中断标志设置为true,仅此而已。被设置中断标志的线程将继续正常运行,不受影响。
interrupt() 并不能真正的中断线程,需要被调用的线程自己进行配合才行,在正常运行任务时,经常检查本线程的中断标志位,如果被设置了中断标志就自行停止线程。具体到底中断还是继续运行,应该由被通知的线程自己处理。
public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (blockerLock) { Interruptible b = blocker; if (b != null) { interrupt0(); // Just to set the interrupt flag b.interrupt(this); return; } } interrupt0(); // Just to set the interrupt flag }
2 interrupted()
检查当前中断标识(即查看当前中断信号是true还是false),并清除中断信号。一般处理过中断以后使用此方法。
public static boolean interrupted() { return currentThread().isInterrupted(true); } / * Tests if some Thread has been interrupted. The interrupted state * is reset or not based on the value of ClearInterrupted that is * passed. */ private native boolean isInterrupted(boolean ClearInterrupted);
3 isInterrupted()
检查当前中断标识(即查看当前中断信号是true还是false)
public boolean isInterrupted() { return isInterrupted(false); }
二 、示例
以下示例展示了通过isInterrupted()、interrupt()如何终止执行中的线程,如下所示代码中中创建了两个线程(thread1、thread2),thread1每隔500ms数数一次,thread2休眠一段时间以后中断thread1,thread1将会终止。
import java.util.Random; / * @author Leon * @create 2019-07-26-15:02 */ public class Thread1 { public static void main(String[] args) { Thread thread1 = new Thread(() -> { try { int i = 0; while (!Thread.currentThread().isInterrupted()) { Thread.sleep(500); System.out.println("couting... i=" + i); i++; } } catch (InterruptedException e) { e.printStackTrace(); System.out.println("thread1 interrupted"); } }); Thread thread2 = new Thread(() -> { Random random = new Random(); try { long time = random.nextInt(3000) + 1000; Thread.sleep(time); System.out.println("interrupt thread1"); thread1.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } }); thread1.start(); thread2.start(); } }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/227591.html原文链接:https://javaforall.net
