当前位置:网站首页>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
边栏推荐
猜你喜欢
线程的6种状态
The solution that does not work and does not take effect after VScode installs ESlint
Getting started with kubernetes apparmor
【Feel】Unity Feel插件中,Camera无法正确显示CameraShake
Unity 五子棋游戏设计和简单AI(2)
C# 利用iTextSharp画PDF
Word文件的只读模式没有密码怎么退出?
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
static静态关键字和继承
How to automatically fill down an excel table without dragging the mouse down
随机推荐
单例模式
golang xml 处理动态属性
DevNet: Deviation Aware Networkfor Lane Detection
jdepend
中英文说明书丨TRC D-阿卓糖(D-Altrose)
【JMeter】jmeter测试 - 上传多个图片/批量上传图片接口 CSV文件参数化方法
深度学习-神经网络原理2
VS2019常用快捷键
vs番茄助手的方便功能和便捷快捷键介绍
【Wwise】ArgumentException: The specified path is not of a legal form (empty).关于WwiseGlobal中的路径读取错误问题
S7-200SMART PLC Modbus TCP communication
Can Jincang Database Set Transaction Automatic Commit?
网络学习总结
sql问题解答创建表的语句
db.sqlite3没有“as Data Source“解决方法
域名批量查询在线工具
flask创建数据库失败未报错
【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
缓存技术使用
中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分