观测线程状态
作者:互联网
观测线程状态
package com.Luoking.Thread;
public class ThreadState {
public static void main(String[] args) throws InterruptedException {
//线程逻辑
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);//线程休眠
} 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(100);
state = thread.getState();
System.out.println(state);
}
System.out.println("啊,,我凉凉了");
}
}
输出
NEW
RUNNABLE
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
RUNNABLE
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
RUNNABLE
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
RUNNABLE
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
我起床了!!
TERMINATED
啊,,我凉凉了
标签:状态,Thread,state,System,WAITING,TIMED,观测线,out 来源: https://www.cnblogs.com/luoking/p/16208128.html