线程状态演示
线程状态演示
操作系统层面有5中状态
Java中有6种状态
NEW、 RUNNABLE、 BLOCKED、 WAITING、 TIMED_WAITING、 TERMINATED
Thread源码中就有显示,打印出来
演示
@Slf4j
public class TState {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
log.debug("t1 running...");
}, "t1");
Thread t2 = new Thread(() -> {
while (true){
}
}, "t2");
t2.start();
Thread t3 = new Thread(() -> {
log.debug("t3 running...");
}, "t3");
t3.start();
Thread t4 = new Thread(() -> {
synchronized (TState.class){
try {
Thread.sleep(10000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t4");
t4.start();
Thread t5 = new Thread(() -> {
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "t5");
t5.start();
Thread t6 = new Thread(() -> {
synchronized (TState.class){
try {
Thread.sleep(10000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "t6");
t6.start();
TimeUnit.MILLISECONDS.sleep(500);
System.out.println("t1 state: " + t1.getState());
System.out.println("t2 state: " + t2.getState());
System.out.println("t3 state: " + t3.getState());
System.out.println("t4 state: " + t4.getState());
System.out.println("t5 state: " + t5.getState());
System.out.println("t6 state: " + t6.getState());
}
}
本文来自博客园,作者:如梦幻泡影,转载请注明原文链接:https://www.cnblogs.com/WangJiQing/p/16993929.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现