观测线程状态

 1 package 多线程练习;
 2 
 3 public class 观测线程状态 {
 4     public static void main(String[] args) {
 5         Runnable runnable = () -> {
 6             for (int i = 0; i < 5; i++) {
 7                 try {
 8                     Thread.sleep(1000);
 9                 } catch (InterruptedException e) {
10                     e.printStackTrace();
11                 }
12             }
13             System.out.println("线程结束");
14         };
15 
16         Thread thread = new Thread(runnable);
17         Thread.State state = thread.getState();
18         System.out.println(state);
19 
20         thread.start();
21         state = thread.getState();
22         System.out.println(state);
23 
24         while (state != Thread.State.TERMINATED) {
25             try {
26                 Thread.sleep(100);
27             } catch (InterruptedException e) {
28                 e.printStackTrace();
29             }
30             state = thread.getState();
31             System.out.println(state);
32         }
33 
34 
35 
36     }
37 }

 

posted @ 2022-01-15 16:16  Chris丶Woo  阅读(17)  评论(0编辑  收藏  举报