package com.Java;

//观测线程状态
public class TestState {
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) {
e.printStackTrace();
}
}
System.out.println("线程执行完毕");
});

Thread.State state = thread.getState();
System.out.println(state); //NEW

thread.start();
state = thread.getState();//Runnable
System.out.println(state);

while (state != Thread.State.TERMINATED) {//线程不停止就一直循环
Thread.sleep(100);
state = thread.getState();//更新线程状态
System.out.println(state); //TIMED_WAITING
}
state = thread.getState();
System.out.println(state); //TERMINATED
thread.start();//线程不能重新启动
}
}