观测线程状态
观测线程状态:Thread.State 线程终止:Thread.State.TERMINATED
![在这里插入图片描述](https://img-blog.csdnimg.cn/8ff43e037cd549c095bcc61c0160f745.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAS19HZW50bGVtYW4=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
Code
public class StateTest {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
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(1000);
state = thread.getState();
System.out.println(state);
}
}
}