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); } } }
浙公网安备 33010602011771号