当前位置:网站首页>6 states of a thread
6 states of a thread
2022-08-09 06:27:00 【The koala over rice】
线程的6种状态
NEW 状态是指线程刚创建, 尚未启动
RUNNABLE 状态是线程正在正常运行中, 当然可能会有某种耗时计算/IO等待的操作/CPU时间片切换等, 这个状态下发生的等待一般是其他系统资源, 而不是锁, Sleep等
BLOCKED 这个状态下, 是在多个线程有同步操作的场景, 比如正在等待另一个线程的synchronized 块的执行释放, 或者可重入的 synchronized块里别人调用wait() 方法, 也就是这里是线程在等待进入临界区
WAITING 这个状态下是指线程拥有了某个锁之后, 调用了他的wait方法, 等待其他线程/锁拥有者调用 notify / notifyAll 一遍该线程可以继续下一步操作, 这里要区分 BLOCKED 和 WATING 的区别, 一个是在临界点外面等待进入, 一个是在理解点里面wait等待别人notify, 线程调用了join方法 join了另外的线程的时候, 也会进入WAITING状态, 等待被他join的线程执行结束
TIMED_WAITING 这个状态就是有限的(时间限制)的WAITING, 一般出现在调用wait(long), join(long)等情况下, 另外一个线程sleep后, 也会进入TIMED_WAITING状态
TERMINATED 这个状态下表示 该线程的run方法已经执行完毕了, 基本上就等于死亡了(当时如果线程被持久持有, 可能不会被回收)
java的Thread类中定义了一个Stateenum inner class,for the above6Each type corresponds to an attribute in the enumeration
// 取自jdk8 Thread源码
public enum State {
/** * Thread state for a thread which has not yet started. */
NEW,
/** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */
RUNNABLE,
/** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */
BLOCKED,
/** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */
WAITING,
/** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */
TIMED_WAITING,
/** * Thread state for a terminated thread. * The thread has completed execution. */
TERMINATED;
}
An example is given6types of appearance
public class TestState {
public static void main(String[] args) {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("t1 running...");
}
};
Thread t2 = new Thread("t2") {
@Override
public void run() {
while (true) {
}
}
};
t2.start();
Thread t3 = new Thread("t3") {
@Override
public void run() {
synchronized (TestState.class) {
try {
sleep(30000000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t3.start();
Thread t4 = new Thread("t4") {
@Override
public void run() {
log.debug("t4 running...");
}
};
t4.start();
Thread t5 = new Thread("t5") {
@Override
public void run() {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t5.start();
Thread t6 = new Thread("t6") {
@Override
public void run() {
synchronized (TestState.class) {
try {
sleep(30000000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t6.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1:" + t1.getState()); // t1:NEW
System.out.println("t2:" + t2.getState()); // t2:RUNNABLE
System.out.println("t3:" + t3.getState()); // t3:TIMED_WAITING
System.out.println("t4:" + t4.getState()); // t4:TERMINATED
System.out.println("t5:" + t5.getState()); // t5:WAITING
System.out.println("t6:" + t6.getState()); // t6:BLOCKED
}
}
Thread switching flowchart
边栏推荐
- db.sqlite3没有“as Data Source“解决方法
- 为什么以太网无法接收大于1500字节的数据包?
- Unity Gobang Game Design and Simple AI(3)
- Initials-Letter Query Tool-Word Abbreviation Query Online Tool
- 常用Oracle命令
- qt send mail program
- crc计算
- Unity 五子棋游戏设计和简单AI(3)
- Output method of list string print(*a) print(““.join(str(c) for c in a) )
- 声母-字母查询工具-词语缩写查询在线工具
猜你喜欢
zip压缩包密码解密
pdf加密、找回密码
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
Polyamide-amine (PAMAM) dendrimer-bismuth sulfide composite nanoparticles | bismuth sulfide modified Gd‑DTPA‑OA ligand | for scientific research
Unity Gobang Game Design and Simple AI (2)
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
【JMeter】jmeter测试 - 上传多个图片/批量上传图片接口 CSV文件参数化方法
运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比
05 多线程与高并发 - ThreadPoolExecutor 源码解析
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
随机推荐
String.toLowerCase(Locale.ROOT)
Qt 学习(三) —— Qt 模块
Service
flask创建数据库失败未报错
数据库中间件-jdbi
Qt learning (3) - Qt module
Polyamide-amine (PAMAM) dendrimer-bismuth sulfide composite nanoparticles | bismuth sulfide modified Gd‑DTPA‑OA ligand | for scientific research
Deep Learning - Principles of Neural Networks 2
GNNExplainer applied to node classification task
CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
Superparamagnetic iron [email protected]@cadmium sulfide nanocore-shell structure material|Fe3O4 magnetic nanop
运放-运算放大器经典应用电路大全-应用电路大全
【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents
phpstudy install flarum forum
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
代码目录结构
Error: flask: TypeError: 'function' object is not iterable
Output method of list string print(*a) print(““.join(str(c) for c in a) )
报错jinja2.exceptions.UndefinedError: ‘form‘ is undefined