當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。