当前位置:网站首页>Thread State 详解
Thread State 详解
2022-08-10 21:11:00 【InfoQ】
前言
絮叨
线程状态转换图

- NEW 初始状态
- RUNNABLE 运行状态
- BLOCKED 阻塞状态
- WAITING 等待状态
- TIME_WAITING 超时等待状态
- TERMINATED 终止状态
阻塞与等待的区别
主要操作
start()
public synchronized void start() {
//判断是否首次启动
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
boolean started = false;
try {
//启动线程
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
private native void start0();
run()
sleep()
sleep(long millis) //参数为毫秒
sleep(long millis,int nanoseconds) //第一参数为毫秒,第二个参数为纳秒
yield()
join()
1 join()
2 join(long millis) //参数为毫秒
3 join(long millis,int nanoseconds) //第一参数为毫秒,第二个参数为纳秒
public final synchronized void join(long millis) throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
//0则需要一直等到目标线程run完
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
//如果目标线程未run完且阻塞时间未到,那么调用线程会一直等待。
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
interrupt()
- 如果线程sleep()、wait()、join()等处于阻塞状态,那么线程会定时检查中断状态位如果发现中断状态位为true,则会在这些阻塞方法调用处抛出InterruptedException异常,并且在抛出异常后立即将线程的中断状态位清除,即重- 新设置为false。抛出异常是为了线程从阻塞状态醒过来,并在结束线程前让程序员有足够的时间来处理中断请求。
- 如果线程正在运行、争用synchronized、lock()等,那么是不可中断的,他们会忽略。
- isInterrupted()
- 此方法只会读取线程的中断标志位,并不会重置。
- interrupted()
- 此方法读取线程的中断标志位,并会重置。
- throw InterruptException
- 抛出该异常的同时,会重置中断标志位。
结尾

日常求赞
边栏推荐
- 力扣215题,数组中的第K个最大元素
- Using SylixOS virtual serial port, serial port free implementation system
- Future-oriented IT infrastructure management architecture - Unified IaaS
- 自建函数 测试例和语法——《mysql 从入门到内卷再到入土》
- LeetCode-498-对角线遍历
- 快消品行业经销商协同系统:实现经销商可视化管理,提高沟通执行效率
- 找的笔试题的复盘(一)
- npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
- 黑猫带你学Makefile第12篇:常见Makefile问题汇总
- F. Binary String Reconstruction
猜你喜欢

Mark!画出漂亮的神经网络图!神经网络可视化工具集锦搜集

用示波器揭示以太网传输机制

数字化转型:如何引导创新领导者

阿里巴巴、蚂蚁集团推出分布式数据库 OceanBase 4.0,单机部署性能超 MySQL

【nvm】【node多版本管理工具】使用说明和踩坑(exit status 1)

Huawei router clock near the drainage experiment (using stream strategy)

Likou 221 questions, the largest square

【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示

深度学习之 12 循环神经网络RNN2

使用SylixOS虚拟串口,实现系统串口自由
随机推荐
数字化转型:如何引导创新领导者
找的笔试题的复盘(一)
FPGA - 7系列 FPGA内部结构之Memory Resources -03- 内置纠错功能
【SQL刷题】Day3----SQL必会的常用函数专项练习
黑猫带你学Makefile第12篇:常见Makefile问题汇总
labelme-5.0.1版本编辑多边形闪退
labelme-屏蔽拖拽的事件
Single-click to cancel the function
美创科技勒索病毒“零信任”防护和数据安全治理体系的探索实践
玩转doxygen 之RT-THREAD
web逆向之丁香园
LeetCode-36-二叉搜索树与双向链表
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
根心与根轴
Detailed explanation and use of each module of ansible
一篇文章教你Pytest快速入门和基础讲解,一定要看
Getting started with kuberentes Auditing
智能方案设计——智能跳绳方案
LeetCode-402 - Remove K digits
【PCBA scheme design】Bluetooth skipping scheme