package com.sxt.state;
import java.lang.Thread.State;
/**
* 观察线程的状态
*
* @author
*
*/
public class AllState {
public static void main(String[] args) {
Thread t = new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("......");
}
});
// 观察状态
State state = t.getState();
System.out.println(state);// NEW
t.start();
state = t.getState();
System.out.println(state);// RUNNABLE
/*
* while(state!=Thread.State.TERMINATED) {
* try {
* Thread.sleep(200);
* } catch(InterruptedException e) {
* e.printStackTrace();
* }
* state =t.getState();//TIMED_WAITING
* System.out.println(state);
* }
* state = t.getState();//TERMINATED
* System.out.println(state);
*/
while (true) {
// 活动的线程数
int num = Thread.activeCount();
if (num == 1) {
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
state = t.getState();
System.out.println(state);// TIMED_WAITING
}
}
}
多线程_深度观察状态
最新推荐文章于 2024-04-30 17:47:51 发布