Thread类下有一个枚举enum类型的变量State,共有六个值
public enum State { NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED; }
NEW对应创建状态。
RUNNABLE对应就绪状态
BLOCKED,WAITING,TIMED_WAITING对应阻塞状态
TERMINATED对应结束状态
package com.design; public class SeeState implements Runnable{ @Override public void run() { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { Thread thread = new Thread(new SeeState()); Thread.State state = thread.getState(); System.out.println(state); thread.start(); state = thread.getState(); System.out.println(state); while (thread.getState()!=Thread.State.TERMINATED) { state = thread.getState(); System.out.println(state); } } }
- 初始化线程后立刻执行getState方法得到的是NEW
- 调用线程的start方法之后再执行getState方法得到的是RUNNABLE
- 线程休眠时得到的State是TIMED_WATING
- 线程结束后倘若再次调用start方法则会抛出异常并显示TERMINED
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)