如何在 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.