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