java.lang.Thread.isAlive()%uA0方法测试线程是活跃的。线程是活跃的,那么它已经启动且尚未死亡。
声明
以下是java.lang.Thread.isAlive()方法的声明
public final boolean isAlive()
参数
-
NA
返回值
如果该线程是活跃的,此方法返回true, 否则返回false。
异常
-
NA
例子
下面的例子显示java.lang.Thread.isAlive()方法的使用。
package com.yiibai 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