Python 官方文档:入门教程 => 点击学习
线程的状态 New 表示线程已创建,没启动的状态此时已经做了一些准备工作,还没有执行run方法中代码 Runnable 调用start方法之后的状态,表示可运行状态(不一定正在运行
线程的状态
New
Runnable
Blocked
Waiting
Timed Waiting
Terminated
代码演示
public class NewRunnableTerminated implements Runnable {
public static void main(String[] args) {
Thread thread = new Thread(new NewRunnableTerminated());
// 打印线程状态
// New
System.out.println(thread.getState());
thread.start();
// Runnable
System.out.println(thread.getState());
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// Runnable
System.out.println(thread.getState());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// TERMINATED
System.out.println(thread.getState());
}
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
System.out.println(i);
}
}
}
public class BlockWaitingTimedWaiting implements Runnable{
public static void main(String[] args) {
BlockWaitingTimedWaiting blockWaitingTimedWaiting = new BlockWaitingTimedWaiting();
Thread thread1 = new Thread(blockWaitingTimedWaiting);
thread1.start();
Thread thread2 = new Thread(blockWaitingTimedWaiting);
thread2.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(thread1.getState());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(thread2.getState());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(thread1.getState());
}
@Override
public void run() {
syn();
}
private synchronized void syn() {
try {
Thread.sleep(1000);
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
阻塞状态
到此这篇关于Java线程生命周期图文详细讲解的文章就介绍到这了,更多相关Java线程生命周期内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java线程生命周期图文详细讲解
本文链接: https://lsjlt.com/news/178424.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0