Java教程

Java isInterrupted()方法

线程类的 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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4