![]()
package ThreadDemo;
// 观测线程状态,五状态
// 死亡过的线程不能再次启动
public class Test15_State {
public static void main(String[] args) throws InterruptedException {
Thread thread =new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("============");
}); // 子线程代理模式
// 观测当前状态
Thread.State state=thread.getState();
System.out.println(state);
thread.start();
state=thread.getState(); // 更新
System.out.println(state);
while (state !=Thread.State.TERMINATED){
Thread.sleep(100);
state=thread.getState(); // 更新
System.out.println(state);
}
thread.start(); // 死亡过的线程不能再次启动
}
}