如果線程處於活動狀態,則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類
下一篇:無