当前位置:网站首页>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
- 抛出该异常的同时,会重置中断标志位。
结尾

日常求赞
边栏推荐
- ArcPy读取Excel时序数据、批量反距离加权IDW插值与掩膜
- Detailed explanation and use of each module of ansible
- ACM模板笔记:八数码问题——使用BFS+康托展开打表解决
- 如何保护 LDAP 目录服务中的用户安全?
- Mark!画出漂亮的神经网络图!神经网络可视化工具集锦搜集
- Before implementing MES management system, these three questions to consider
- Single-click to cancel the function
- 《mysql 从入门到内卷再到入土》——Mysql基础 学习笔记目录
- Getting started with kuberentes Auditing
- HGAME 2022 Week2 writeup by pankas
猜你喜欢
JVM classic fifty questions, now the interview is stable
Play RT-THREAD of doxygen
【实用软件】【VSCode】使用技巧大全(持续更新)
石油化工行业商业供应链管理系统:标准化供应商管理,优化企业供应链采购流程
LeetCode-402 - Remove K digits
社区分享|货拉拉通过JumpServer纳管大规模云上资产
The use of TortoiseSVN little turtle
接口测试的概念、目的、流程、测试方法有哪些?
Huawei router clock near the drainage experiment (using stream strategy)
npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
随机推荐
PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》
Redis Command Manual
力扣221题,最大正方形
Intelligent scheme design - intelligent rope skipping scheme
Before implementing MES management system, these three questions to consider
Huawei router clock near the drainage experiment (using stream strategy)
MATLAB神经网络拟合工具箱Neural Net Fitting使用方法
社区分享|货拉拉通过JumpServer纳管大规模云上资产
JVM classic fifty questions, now the interview is stable
自组织是管理者和成员的双向奔赴
Floating window in Auto.js
ENVI感兴趣区ROI文件由XML格式转为ROI格式
labelme - block drag and drop events
【Windows】你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问,这些策略可帮助保护你的电脑
内置模板市场,DataEase开源数据可视化分析平台v1.13.0发布
直播课堂系统09--腾讯云点播管理模块(一)
web逆向之丁香园
黑猫带你学Makefile第13篇:Makefile编译问题合集
着力提升制造业核心竞争力,仪器仪表产业迎高质量发展
Redis 性能影响 - 异步机制和响应延迟