博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中interrupted()和isInterrupted
阅读量:4228 次
发布时间:2019-05-26

本文共 1583 字,大约阅读时间需要 5 分钟。

interrupted():测试当前线程是否已经中断(当前正在执行的线程,是静态方法)。

isInterrupted():测试线程是否已经中断。(对象线程)。

interrupted()方法具有清除状态的功能,isInterrupted()并未清除状态标志。

示例:

public class Run {    public static void main(String[] args){            MyThread thread=new MyThread();            thread.start();        try {            Thread.sleep(10);        } catch (InterruptedException e) {            e.printStackTrace();        }        thread.interrupt();            System.out.println("是否停止1?="+MyThread.interrupted());            System.out.println("是否停止2?="+MyThread.interrupted());        System.out.println("end!");    }}
i=1
i=2
i=3
i=4
i=5
是否停止1?=false
是否停止2?=false
end!

因为打断的是thread线程,而正在执行的是main线程,所以返回false

改成打断主线程就可以了:

public class Run {    public static void main(String[] args){            MyThread thread=new MyThread();            thread.start();        try {            Thread.sleep(10);        } catch (InterruptedException e) {            e.printStackTrace();        }        Thread.currentThread().interrupt();            System.out.println("是否停止1?="+MyThread.interrupted());            System.out.println("是否停止2?="+MyThread.interrupted());        System.out.println("end!");    }}

i=1

i=2
i=3
i=4
i=5
是否停止1?=true
是否停止2?=false
end!

public class Run {    public static void main(String[] args){        MyThread thread=new MyThread();        thread.start();        thread.interrupt();        System.out.println("是否停止1 ? ="+thread.isInterrupted());        System.out.println("是否停止2 ? ="+thread.isInterrupted());        System.out.println("end!");    }}
是否停止1 ? =true
i=1
是否停止2 ? =true
end!
i=2
i=3
i=4
i=5

转载地址:http://pojqi.baihongyu.com/

你可能感兴趣的文章
Dual-Primal Graph Convolutional Networks 对偶-原始图卷积神经网络
查看>>
GoGNN: Graph of Graphs Neural Network for Predicting Structured Entity Interactions
查看>>
Estimating Node Importance in Knowledge Graphs Using Graph Neural Networks
查看>>
DiffPool: Hierarchical Graph Representation Learning with Differentiable Pooling
查看>>
MuchGCN:Multi-Channel Graph Convolutional Networks
查看>>
kernel_size为1的卷积核与全连接层的关系
查看>>
STRATEGIES FOR PRE-TRAINING GRAPH NEURAL NETWORKS
查看>>
PAT_A 1010. Radix (25)
查看>>
PAT_A 1005. Spell It Right (20)
查看>>
PAT_A 1012. The Best Rank (25)
查看>>
PAT_A 1013. Battle Over Cities (25)
查看>>
PAT_A 1015. Reversible Primes (20)
查看>>
SetWindowLong函数介绍
查看>>
百度云cdn,bos设置
查看>>
[chrome]好用的chrome Json 格式化插件
查看>>
[Android]hex 64k解决
查看>>
[iphone]调出来控制的小圆球(控制点)
查看>>
[react-native]prop,state对比
查看>>
ssl问题被google 拒收
查看>>
[GreenDAO]like的坑
查看>>