美文网首页
Thread.interrupted & Thread.curr

Thread.interrupted & Thread.curr

作者: K_a921 | 来源:发表于2018-11-14 15:03 被阅读0次

    源码:

    Thread.interrupted

    public static boolean interrupted() {

    return currentThread().isInterrupted(true);

    }

    Thread.currentThread().isInterrupted():

    public boolean isInterrupted() {

    return isInterrupted(false);

    }

    最后调用底层,从参数名可以看出,interrupted静态方法会擦出线程的中断标志,而Thread.currentThread().isInterrupted()不会执行擦除。

    private native boolean isInterrupted(boolean ClearInterrupted);

    代码验证:

    //输出当前线程的中断标志

    System.out.println(Thread.currentThread().isInterrupted());

    //标记中断

    Thread.currentThread().interrupt();

    //输出当前线程的中断标志

    System.out.println(Thread.currentThread().isInterrupted());

    //输出当前线程中断标志并擦除

    System.out.println(Thread.interrupted());

    //再次输出当前线程的中断标志

    System.out.println(Thread.currentThread().isInterrupted());

    结果输出:

    false

    true

    true

    false

    相关文章

      网友评论

          本文标题:Thread.interrupted & Thread.curr

          本文链接:https://www.haomeiwen.com/subject/iohjfqtx.html