如果线程处于活动状态,则Thread
类的isAlive()
方法进行测试。当调用Thread
类的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
上一篇:
Java Runtime类
下一篇:无