当前位置:网站首页>The Thread State,
The Thread State,
2022-08-10 22:04: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");
}
//0You need to wait until the target threadrun完
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
//If the target thread is notrunis finished and the blocking time has not expired,Then the calling thread will wait forever.
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
interrupt()
- 如果线程sleep()、wait()、join()等处于阻塞状态,那么线程会定时检查中断状态位如果发现中断状态位为true,则会在这些阻塞方法调用处抛出InterruptedException异常,并且在抛出异常后立即将线程的中断状态位清除,即重- New set tofalse.抛出异常是为了线程从阻塞状态醒过来,并在结束线程前让程序员有足够的时间来处理中断请求.
- 如果线程正在运行、争用synchronized、lock()等,那么是不可中断的,他们会忽略.
- isInterrupted()
- 此方法只会读取线程的中断标志位,并不会重置.
- interrupted()
- 此方法读取线程的中断标志位,并会重置.
- throw InterruptException
- 抛出该异常的同时,会重置中断标志位.
结尾
日常求赞
边栏推荐
- [Maui official version] Create a cross-platform Maui program, as well as the implementation and demonstration of dependency injection and MVVM two-way binding
- TCL:事务的特点,语法,测试例——《mysql 从入门到内卷再到入土》
- ACM解题笔记——HDU 1401 Solitaire(DBFS)
- 边缘与云计算:哪种解决方案更适合您的连接设备?
- String类的常用方法
- 一篇文章教你Pytest快速入门和基础讲解,一定要看
- 接口测试的概念、目的、流程、测试方法有哪些?
- 【PCBA方案设计】蓝牙跳绳方案
- 直播课堂系统09--腾讯云点播管理模块(一)
- 这些不可不知的JVM知识,我都用思维导图整理好了
猜你喜欢
HighTec快捷键(Keys)设置位置
PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
ThreadLocal comprehensive analysis (1)
12 Recurrent Neural Network RNN2 of Deep Learning
【PCBA方案】电子握力测试仪方案she‘ji
一篇文章教你Pytest快速入门和基础讲解,一定要看
An article to teach you a quick start and basic explanation of Pytest, be sure to read
Likou 221 questions, the largest square
Shell编程规范与变量
随机推荐
2022.8.9 Mock Competition
An article to teach you a quick start and basic explanation of Pytest, be sure to read
LeetCode-36-二叉搜索树与双向链表
labelme-屏蔽拖拽的事件
黑猫带你学Makefile第13篇:Makefile编译问题合集
DELETE:删除操作语法&使用例——《mysql 从入门到内卷再到入土》
RTL8721DM 双频WIFI + 蓝牙5.0 物联网(IoT)应用
2022.8.8 Selected Lectures on Good Topics (Number Theory Field)
Live Classroom System 09--Tencent Cloud VOD Management Module (1)
CGO Preliminary Cognition and Basic Data Type Conversion
LeetCode-402 - Remove K digits
Likou 215 questions, the Kth largest element in an array
VLAN huawei 三种模式
基于Pix4Dmapper的运动结构恢复法无人机影像三维模型重建
Shell programming specification and variables
TCL:事务的特点,语法,测试例——《mysql 从入门到内卷再到入土》
从斐波那契 - 谈及动态规划 - 优化
ThreadLocal comprehensive analysis (1)
3D model reconstruction of UAV images based on motion structure restoration method based on Pix4Dmapper
黑猫带你学Makefile第11篇:当头文件a.h改变时,如何将所有依赖头文件a.h的.c文件都重新编译