11、查看线程状态

11、线程状态

package com.testthread1;

public class TestThread  {
    public static void main(String[] args) {
        //线程使用lambda表达式
        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 枚举
        Thread.State state= thread.getState();
        System.out.println(state);

        //启动线程
        thread.start();
        state=thread.getState();
        System.out.println(state);//run

        while (state!=Thread.State.TERMINATED){
            try {
                Thread.sleep(200);
            }catch (InterruptedException e)     {
                e.printStackTrace();
            }
            state = thread.getState();//更新线程状态
            System.out.println(state);

        }
    }
}

 

posted @ 2022-07-17 14:46  颓废且努力奋斗的人  阅读(139)  评论(0)    收藏  举报