笔记20200521004:多线程【线程状态】
package com.chengguo.线程; /** * 观察线程的状态 */ public class Demo_20200518005_State { public static void main(String[] args) throws InterruptedException { //创建线程,通过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 state = thread.getState(); System.out.println(state);//NEW // 观察启动后 thread.start(); state = thread.getState(); System.out.println(state);//RUN //只要线程不终止,就一直输出状态 while (state != Thread.State.TERMINATED) { Thread.sleep(100); //更新线程状态 state = thread.getState(); //输出状态 System.out.println(state); } } }