boolean isAlive()
描述 (Description)
java.lang.Thread.isAlive()方法测试此线程是否处于活动状态。 如果一个线程已经启动并且还没有死亡,它就是活着的。
声明 (Declaration)
以下是java.lang.Thread.isAlive()方法的声明
public final boolean isAlive()
参数 (Parameters)
NA
返回值 (Return Value)
如果此线程处于活动状态,则此方法返回true,否则返回false。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.Thread.isAlive()方法的用法。
package com.iowiki;
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