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)和 ...
随机推荐
- Educational Codeforces Round 43 E&976E. Well played! 贪心
传送门:http://codeforces.com/contest/976/problem/E 参考:https://www.cnblogs.com/void-f/p/8978658.html 题意: ...
- 牛客OI测试赛 C 序列 思维
链接:https://www.nowcoder.com/acm/contest/181/C来源:牛客网 题目描述 小a有n个数,他想把他们划分为连续的权值相等的k段,但他不知道这是否可行. 每个数都必 ...
- Atcoder C - Closed Rooms(思维+bfs)
题目链接:http://agc014.contest.atcoder.jp/tasks/agc014_c 题意:略. 题解:第一遍bfs找到所有可以走的点并标记步数,看一下最少几步到达所有没锁的点,然 ...
- 【Spring】 AOP Base
1. AOP概述 2. AOP的术语: 3. AOP底层原理 4. Spring 中的AOP 4.1 概述 4.2 分类 4.3 Spring的传统AOP 针对所有方法的增强:(不带有切点的切面) 带 ...
- Nginx总结(六)nginx实现负载均衡
前面讲了如何配置Nginx虚拟主机,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天要 ...
- android 之下拉刷新
一.概述 Android 下拉刷新几乎是每个应用都必带的功能, 并且现在下拉刷新第三方库也越来越多了,很方便就能实现该功能, 下面我介绍一下 自己常用的几个方法. 二.例子 第一种方式:就是集成Lis ...
- Winform中设置ZedGraph曲线图的水平与竖直参考线
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- PHP秒杀系统 高并发 高性能的极致挑战 下载
第1章 课程介绍 秒杀系统在各种网站和应用中经常会用到.本课程从基本的系统设计和基础功能开始教导大家用PHP来设计和实现秒杀系统,并且为海量并发提供更高级的技术方案和实现手段. 第2章 系统技术选型分 ...
- 安装Winservices服务出现“设置服务登录”
安装服务时出现 设置服务登录 窗口 别紧张 将serviceProcessInstaller1 Account 设置为LocalSystem 即可
- [Advanced Python] 16 - Google style guide for programming
Ref: Python 风格指南 - 内容目录 这里主要记录一下值得注意的地方. Python语言规范 Lint:vsCode自带 导入:完整路径 异常 Ref: [Python] 07 - Stat ...