当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Thread isInterrupted()用法及代码示例


线程类的isInterrupted()方法是测试线程是否被中断的实例方法。它返回内部标志的值,真或假。如果线程被中断,那么它将返回真,否则返回假。

用法

public boolean isInterrupted()

返回

如果线程被中断则返回真,否则返回假。

示例

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 Thread isInterrupted() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。