Java 学习笔记之 线程interrupted方法
线程interrupted方法:
interrupted()是Thread类的方法,用来测试当前线程是否已经中断。
public class InterruptThread extends Thread{
@Override
public void run() {
for (int i = 0; i< 5000000; i++){
System.out.println("i=" + (i + 1));
}
}
}
public class ThreadRunMain {
public static void main(String[] args) {
testInterruptThread();
}
public static void testInterruptThread(){
try {
InterruptThread it = new InterruptThread();
it.start();
Thread.sleep(1000);
it.interrupt();
System.out.println("First call: " + Thread.interrupted());
System.out.println("Second call: " + Thread.interrupted());
} catch (InterruptedException e) {
System.out.println("Main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
运行结果:

从控制台打印的结果来看,返回的结果是false,因为当前线程是main,被中断的却是InterruptThread,所以main线程不受影响。
再看一个例子:
public class ThreadRunMain {
public static void main(String[] args) {
testMainInterruptThread();
}
public static void testMainInterruptThread(){
Thread.currentThread().interrupt();
System.out.println("First call: " + Thread.interrupted());
System.out.println("Second call: " + Thread.interrupted());
System.out.println("end!");
}
}
运行结果:

同样是调用Thread.interrupted(), 但是为什么第一次结果是true,第二次确是false呢?
因为interrupted()方法具有清除状态功能,所以第二次调用interrupted()返回的值是false。
Java 学习笔记之 线程interrupted方法的更多相关文章
- Java 学习笔记之 线程interrupt方法
线程interrupt方法: interrupt方法是用来停止线程的,但是他的使用效果并不像for+break那样,马上就停止循环. 调用interrupt()其实仅仅是在当前线程中打了一个停止标记, ...
- Java 学习笔记之 线程isInterrupted方法
线程isInterrupted方法: isInterrupted()是Thread对象的方法,测试线程是否已经中断. public class ThreadRunMain { public stati ...
- Java 学习笔记之 线程sleep方法
线程sleep方法: 单主线程使用sleep: Main线程差了2000毫秒. public class MainSleepThread extends Thread{ @Override publi ...
- 0040 Java学习笔记-多线程-线程run()方法中的异常
run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...
- Java 学习笔记之 线程isAlive方法
isAlive方法: 方法isAlive()功能是判断当前线程是否处于活动状态. 活动状态就是线程启动且尚未终止,比如正在运行或准备开始运行. public class IsAliveThread e ...
- java学习笔记15--多线程编程基础2
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...
- java学习笔记14--多线程编程基础1
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...
- java学习笔记之线程(Thread)
刚开始接触java多线程的时候,我觉得,应该像其他章节的内容一样,了解了生命周期.构造方法.方法.属性.使用的条件,就可以结束了,然而随着我的深入学习了解,我发现java的多线程是java的一个特别重 ...
- JAVA学习笔记16——线程生命周期
当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态,在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞(Blocking)和 ...
随机推荐
- J-Subarray_2019牛客暑期多校训练营(第二场)
题意 有一个只由1,-1组成的数组,给出所有连续的1所在位置,求满足1的个数大于-1的个数的子区间的数量 题解 参考博客:https://www.cnblogs.com/Yinku/p/1122149 ...
- 2019 Multi-University Training Contest 8
2019 Multi-University Training Contest 8 C. Acesrc and Good Numbers 题意 \(f(d,n)\) 表示 1 到 n 中,d 出现的次数 ...
- 牛客小白月赛6 C 桃花 dfs 求树上最长直径
链接:https://www.nowcoder.com/acm/contest/136/C来源:牛客网 题目描述 桃花一簇开无主,可爱深红映浅红. ...
- SpringBoot + JPA问题汇总
实体类有继承父类,但父类没有单独标明注解 异常表现 Caused by: org.hibernate.AnnotationException: No identifier specified for ...
- SQL查询出距当前时间最近的一条或多条记录。
select * from bas_dredge,(SELECT C_ENTERPRISEID,MAX(D_UTIME) D_LTIME FROM BAS_DREDGE GROUP BY C_ENTE ...
- android 中Application的作用
转自:lieren666 博客地址:http://blog.csdn.net/lieren666/article/details/7598288 What is Application Applica ...
- ThreadPoolTaskExecutor介绍
ThreadPoolTaskExecutor是一个spring的线程池技术,其实,它的实现方式完全是使用ThreadPoolExecutor进行实现.对于ThreadPoolExecutor,有一些重 ...
- .Net基础篇_学习笔记_第五天_流程控制do-while循环
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans
I have been suffering from infamous hibernate exception org.hibernate.LazyInitializationException: c ...
- Centos第一次使用配置IP地址
1.vim /etc/sysconfig/network-scripts/ifcfg-eth0 修改默认配置文件 TYPE=Ethernet BOOTPROTO=static #静态 可修改为[n ...