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