线程中断方法Thread.currentThread().interrupt()、Thread.interrupted()、Thread.currentThread().isInterrupted()
线程中断方法Thread.currentThread().interrupt()、Thread.interrupted()、Thread.currentThread().isInterrupted()
本文介绍3个线程中断方法的意义及使用结果。在ReentrantLock类中会看到有使用的地方。
-
Thread.currentThread().interrupt():表示为当前线程打中断标记。
-
Thread.interrupted():表示清除中断标记,如果当前线程中断,返回true,否则返回false
-
Thread.currentThread().isInterrupted():表示查看当前线程的状态是否中断,不清除中断标记
测试代码
- public static void main(String[] args) {
- System.out.println("线程开始状态1"+Thread.currentThread().isInterrupted());
- // 为线程打中断标记
- Thread.currentThread().interrupt();
- // 当前线程状态是否中断,上一行打中断标记后就是true
- System.out.println("线程打中断标记状态2"+Thread.currentThread().isInterrupted());
- //获取线程中断状态true,并清除
- boolean interrupted = Thread.interrupted();
- System.out.println("中断标记"+interrupted);
- // 清除完中断标记,中断状态false
- System.out.println("线程清除中断标记后状态3"+Thread.currentThread().isInterrupted());
- // 线程阻塞;线程中断过,这里就不会阻塞,继续往下执行
- LockSupport.park();
- boolean interrupted2 = Thread.interrupted();
- System.out.println("继续运行"+interrupted2);
-
- }
结果输出图:
这里注意:
LockSupport.park();
如果在此之前标记过中断,这里不会阻塞,会继续执行。
情况2:
注释第2行代码,再看运行情况
应用场景
- // java.util.concurrent.locks.AbstractQueuedSynchronizer#acquireQueued
-
- final boolean acquireQueued(final Node node, int arg) {
- boolean failed = true;
- try {
- boolean interrupted = false;
- for (;;) {
- final Node p = node.predecessor();
- if (p == head && tryAcquire(arg)) {
- setHead(node);
- p.next = null; // help GC
- failed = false;
- return interrupted;
- }
- if (shouldParkAfterFailedAcquire(p, node) &&
- parkAndCheckInterrupt())
- interrupted = true;
- }
- } finally {
- if (failed)
- cancelAcquire(node);
- }
- }
-
- static void selfInterrupt() {
- Thread.currentThread().interrupt();
- }
-
-
-
- private final boolean parkAndCheckInterrupt() {
- // 阻塞住,等待被唤醒
- LockSupport.park(this);
- //清除中断标记,下次线程还会被阻塞,这里返回false
- return Thread.interrupted();
- }