前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >012.多线程-interrupt线程的中断

012.多线程-interrupt线程的中断

作者头像
qubianzhong
发布2018-12-13 17:00:49
8130
发布2018-12-13 17:00:49
举报
文章被收录于专栏:行者常至行者常至

版权声明:本文为博主原创文章,允许转载,请标明出处。

interrupt()

中断线程, 具体使用场景可以查看下面的源码以及注释

    /**
     * 中断线程
     *
     * 除非当前线程中断自身,
     * 否则在checkAccess中将会抛出SecurityException
     *
     * 如果当前线程在 wait、join、sleep 中被阻塞,
     * 将会清除它的中断状态(isInterrupted() is false),
     * 并抛出InterruptedException
     *
     * 如果当前线程在java.nio.channels上的I/O操作被阻塞,
     * 则通道将被关闭,线程的中断状态被设置(isInterrupted() is true),
     * 并抛出ClosedByInterruptException
     *
     * 如果当前线程在java.nio.channels.Selector中被阻塞,
     * 则线程的中断状态被设置(isInterrupted() is true),
     * 并立即返回,可能带有非零值。
     * 类似于调用了java.nio.channels.Selector的wakeup方法
     *
     * 如果上面的条件都不存在,
     * 则线程的中断状态被设置(isInterrupted() is true)
     *
     */
    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();
    }

isInterrupted()

    /**
     * 测试这个线程是否被中断,
     * 但是线程的中断状态,不受此方法的影响
     *
     * 当一个线程死亡的时候,
     * 设置线程的中断状态将会被忽略,
     * 调用此方法将返回false
     *
     * 如果这个线程被中断(即:中断状态被设置)
     * 则返回true,否则,返回false。
     */
    public boolean isInterrupted() {
        return isInterrupted(false);
    }

interrupted()

    /**
     * 测试当前线程是否中断
     * 返回结果与isInterrupted返回值一样,
     * 
     * 但该方法会清除线程的中断状态
     * 
     * 即:不过当前线程状态如何,如果连续调用两次此方法,
     * 则,第二次调用将返回false。
     * 因为,第一次调用时已清除了中断状态。
     */
    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }

code of demo:

package cn.qbz.thread;

import org.springframework.validation.annotation.Validated;

/**
 * @Author: 花生米
 * @Date: 2018/11/16 10:20
 */
public class InterruptTest {

    public static void main(String[] args) throws InterruptedException {
        Test2 test2 = new Test2();
        System.out.println("\n线程未启动之前:" + test2.isInterrupted());
        test2.start();
        System.out.println("\n调用interrupt前:" + test2.isInterrupted());

        test2.interrupt();
        System.out.println("\n调用interrupt,中断状态被设置:" + test2.isInterrupted());

        while (true) {
            if (test2.isAlive()) {
                Thread.sleep(2000);
            } else {
                break;
            }
        }
        System.out.println("\n线程已死亡:" + test2.isInterrupted());
    }

}

class Test2 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 600001; i++) {
            System.out.print("");

            if (i == 600000) {
                System.out.println("interrupted:" + Thread.interrupted());
                System.out.println("interrupted late::" + isInterrupted());
            }
        }
        System.out.println("线程即将死亡...");
        
        interrupt();
        System.out.println("interrupted late::" + isInterrupted());
    }

}

注意事项:

interrupted 测试的时当前线程; isInterrupted 测试的时被调用的线程。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • interrupt()
  • isInterrupted()
  • interrupted()
  • code of demo:
  • 注意事项:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档