Java.lang.Thread.isAlive() 方法
描述
java.lang.Thread.isAlive() 方法测试该线程是否处于活动状态。 如果线程已启动且尚未死亡,则该线程处于活动状态。
声明
以下是 java.lang.Thread.isAlive() 方法的声明。
public final boolean isAlive()
参数
NA
返回值
如果此线程处于活动状态,则此方法返回 true,否则返回 false。
异常
NA
示例
下面的例子展示了 java.lang.Thread.isAlive() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ThreadDemo implements Runnable {
public void run() {
Thread t = Thread.currentThread();
// tests if this thread is alive
System.out.println("status = " + t.isAlive());
}
public static void main(String args[]) throws Exception {
Thread t = new Thread(new ThreadDemo());
// this will call run() function
t.start();
// waits for this thread to die
t.join();
// tests if this thread is alive
System.out.println("status = " + t.isAlive());
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
status = true status = false

