Thread
类的interrupted()
方法用于检查当前线程是否已被中断。 此方法清除线程的中断状态,这意味着如果要连续两次调用此方法,则第二次调用将返回false
。 如果线程的中断状态为true
,则此方法将状态设置为false
。
语法
public static boolean interrupted()
返回
如果当前线程已被中断,则此方法将返回true
,否则返回false
。
示例
public class JavaInterruptedExp 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
{
JavaInterruptedExp t1=new JavaInterruptedExp();
JavaInterruptedExp t2=new JavaInterruptedExp();
// call run() method
t1.start();
t2.start();
System.out.println("is thread t1 interrupted..: "+t1.interrupted());
// interrupt thread t1
t1.interrupt();
System.out.println("is thread t1 interrupted..: " +t1.interrupted());
System.out.println("is thread t2 interrupted..: "+t2.interrupted());
}
}
执行上面示例代码,得到以下结果:
is thread t1 interrupted..: false
is thread t1 interrupted..: false
is thread t2 interrupted..: false
doing task....: 1
doing task....: 2
doing task....: 3
doing task....: 1
doing task....: 2
doing task....: 3
上一篇:
Java Runtime类
下一篇:无