其他分享
首页 > 其他分享> > 十七、观测线程状态

十七、观测线程状态

作者:互联网

 线程可以处于以下状态之一:

 

public class ThreadStates{

    public static void main(String[] args) throws InterruptedException {

        Thread thread=new Thread(()->{
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("run");
        });

        System.out.println(thread.getState()); //NEW

        thread.start();
        System.out.println(thread.getState()); //RUNNABLE

        while (thread.getState()!=Thread.State.TERMINATED){
            System.out.println(thread.getState());
        }
        System.out.println(thread.getState());
    }
}

结果:

 

标签:状态,十七,getState,thread,System,线程,println,观测线,out
来源: https://www.cnblogs.com/epiphany8/p/16271670.html