日志文章

2019-12-26 aflfte2011

观察线程的状态

package com.aflfte.state;

import java.lang.Thread.State;
import java.time.chrono.ThaiBuddhistEra;

/**
 * 观察线程的状态
 * 
 * @author jinhao
 *
 */
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) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("-->>---");
}

}) ;
//观察状态
State state=t.getState();
System.out.println(state);
t.start();
/*while(state!=Thread.State.TERMINATED) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
state=t.getState();
System.out.println(state);
}*/
while(true) {
int num=Thread.activeCount();
System.out.println(num);
if(num==1) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
state=t.getState();
System.out.println(state);
}

}

}


« 线程的优先级 | 线程合并(线程插队)»