Thread
類的isInterrupted()
方法是一個實例方法,用於測試線程是否已被中斷。 它返回內部標誌的值true
或false
。 如果線程被中斷,那麼它將返回true
,否則返回false
。
public boolean isInterrupted()
返回
如果線程已被中斷,它將返回true
,否則返回false
。
示例
public class JavaIsInterruptedExp extends Thread
{
public void run()
{
for(int i=1;i<=3;i++)
{
System.out.println("doing task....: "+i);
}
}
public static void main(String args[])throws InterruptedException
{
JavaIsInterruptedExp t1=new JavaIsInterruptedExp();
JavaIsInterruptedExp t2=new JavaIsInterruptedExp();
// call run() method
t1.start();
t2.start();
System.out.println("is thread interrupted..: "+t1.isInterrupted());
System.out.println("is thread interrupted..: "+t2.isInterrupted());
// interrupt thread t1
t1.interrupt();
System.out.println("is thread interrupted..: " +t1.isInterrupted());
System.out.println("is thread interrupted..: "+t2.isInterrupted());
}
}
執行上面示例代碼,得到以下結果:
is thread interrupted..: false
is thread interrupted..: false
is thread interrupted..: true
is thread interrupted..: false
doing task....: 1
doing task....: 2
doing task....: 3
doing task....: 1
doing task....: 2
doing task....: 3
上一篇:
Java Runtime類
下一篇:無