当前位置:网站首页>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
边栏推荐
- 中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
- 22 high mid term paper topics forecast
- golang xml 处理动态属性
- C language implements sequential stack and chain queue
- 一道很简答但是没答对的SQL题
- VB.net程序关闭后后台还在与SQL连接
- Remember a nest.js route that matches all the path problems that follow
- Data center project preliminary summary
- Word文件的只读模式没有密码怎么退出?
- 【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
猜你喜欢
中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
Teach you how to make the Tanabata meteor shower in C language - elegant and timeless (detailed tutorial)
缓存技术使用
什么是excel文件保护
2022-08-08: Given an array arr, it represents the height of the missiles that will appear in order from morning to night.When the cannon shoots missiles, once the cannon is set to shoot at a certain h
Unity Gobang Game Design and Simple AI (2)
Unity 五子棋游戏设计和简单AI(2)
phpstudy install flarum forum
Invalid argument(s) appears when redis runs lua script
运放-运算放大器经典应用电路大全-应用电路大全
随机推荐
Polyamide-amine (PAMAM) dendrimer-bismuth sulfide composite nanoparticles | bismuth sulfide modified Gd‑DTPA‑OA ligand | for scientific research
S7-200SMART PLC Modbus TCP通信
TCP segment of a reassembled PDU
Redis 2 - 高级
默默重新开始,第一页也是新的一页
BeautifulSoup4的介绍与使用
Introduction to AIOT
剑指offer专项突击版第24天
【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
阿里巴巴官方技术号
22 high mid term paper topics forecast
Unity backgammon game design and simple AI implementation (1)
db.sqlite3没有“as Data Source“解决方法
[R language] interaction test data
String.toLowerCase(Locale.ROOT)
phpstudy 安装 flarum论坛
[MySQL]二、进程的关系、MySQL密码破解、建表和建库相关命令
shardingsphere数据分片配置项说明和示例
DevNet: Deviation Aware Networkfor Lane Detection