线程状态State/activeCount
package TCPUDPThread;
import java.lang.Thread.State;
/**
* 观察线程的状态
* wait() 同步阻塞
* synchronized 等待阻塞
* 死亡状态不能在切回到新生状态
*/
public class statusTest6 {
public static void main(String[] args) throws Exception {
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();
/**
* 1.NEW 新生状态
*/
System.out.println(state);
/**
* 2.RUNNABLE 运行中
*/
t.start();
state=t.getState();
System.out.println(state);
/**
* 3.TIMED_WAITING 阻塞
*/
while(state!=Thread.State.TERMINATED){
/**
* 活动的线程数 activeCount();
*/
int num=Thread.activeCount();
if(num==1){
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
state=t.getState();
System.out.println(state);
}
/**
* 4.TERMINATED 结束
*/
state=t.getState();
System.out.println(state);
}
}