如何在 Java 中使用 Thread 類的 isAlive() 方法?


Thread 類的isAlive()方法只要執行緒處於執行狀態,即從開始執行緒啟動一直到執行完成,都返回 true。

示例

class first implements Runnable {
   public void run() {
      try {
         for(int i=0; i<=20; i+=2) {
            Thread.sleep(600); System.out.println(i);
         }
      } catch(Exception e) {
   }
}

class MyThread {
   public static void main(String args[]) {
      first obj = new first();
      Thread thread = new Thread(obj);
      thread.setPriority(Thread.MAX_PRIORITY);
      thread.start();
      System.out.println(thread.isAlive());
   }
}

輸出

true
0
2
4
6
8
10
12
14
16
18
20

更新於: 2020-02-20

274 瀏覽

開啟你的職業生涯

完成課程認證

開始
廣告
© . All rights reserved.