深度观察线程状态
【小城贝尔】
线程生命有轨迹,
API中寻机密。
新生new字状态类,
get方法来买醉。
RUN后NABLE两种态,
运行就绪均涵盖。
睡眠加入时间待,
等待流向是阻塞。
public class ThreadState {
public static void main(String[] args) {
boolean flag = true;
Thread t = new Thread(()->{
for (int i = 0; i <=20; i ++){
System.out.println("learning state .....");
if(i % 4 == 0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
System.out.println("thread's state ....is ... "+t.getState());
t.start();
System.out.println("thread's state ....is ... "+t.getState());
while (flag){
if(t.getState().toString().equals("TERMINATED")){
break;
}
System.out.println("around get State ..."+t.getState());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/*
thread's state ....is ... NEW 新生状态
thread's state ....is ... RUNNABLE 运行状态 包含就绪状态 和运行状态
around get State ...RUNNABLE
learning state .....
around get State ...TIMED_WAITING 阻塞状态 包含sleep方法 join方法
learning state .....
learning state .....
learning state .....
learning state .....
learning state .....
learning state .....
learning state .....
learning state .....
around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
learning state .....
learning state .....
learning state .....
learning state .....
around get State ...TIMED_WAITING 阻塞状态 包含sleep方法 join方法
learning state .....
around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
learning state .....
learning state .....
learning state .....
learning state .....
learning state .....
learning state .....
learning state .....
around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
BLOCKED :阻塞状态包含 wait ,流操作read write方法等
*/
}