Thread.State
import java.lang.Thread.State;
public class ThreadState {
public static void main(String[] args) {
Thread t = new Thread(()->{
for(int i=0;i<20;i++) {
try {
Thread.sleep(10);
}catch(InterruptedException e) {
e.printStackTrace();
}
Thread h = new Thread(new MyThread());
if(i==15) {
h.start();
try {
h.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Running---->"+i);
}
});
State s = t.getState(); //NEW
System.out.println(s);
t.start();
s = t.getState(); //RUNNABLE
System.out.println(s);
while(true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
s = t.getState();//出现WAITING和WIMED_WAITING
if(State.TERMINATED==s)break;
System.out.println(s);
}
s = t.getState();//线程退出 TERMINATED
System.out.println(s);
}
}
class MyThread extends Thread{
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("the child thread is Running....");
}
}
原文:https://www.cnblogs.com/xiaokw/p/12678782.html