Java isAlive()方法
线程类的
isAlive()方法测试线程是否处于活动状态。当调用线程类的start()方法并且该线程尚未死亡时,该线程被视为活动线程。如果线程仍在运行但尚未完成,则此方法返回true。
语法
public final boolean isAlive()
返回
如果线程处于活动状态,则此方法将返回true,否则返回false。
示例
public class JavaIsAliveExp extends Thread
{
public void run()
{
try
{
Thread.sleep(300);
System.out.println("is run() method isAlive "+Thread.currentThread().isAlive());
}
catch (InterruptedException ie) {
}
}
public static void main(String[] args)
{
JavaIsAliveExp t1 = new JavaIsAliveExp();
System.out.println("before starting thread isAlive: "+t1.isAlive());
t1.start();
System.out.println("after starting thread isAlive: "+t1.isAlive());
}
}
输出:
before starting thread isAlive: false
after starting thread isAlive: true
is run() method isAlive true