亲宝软件园·资讯

展开

java isInterrupted()如何判断线程 java isInterrupted()判断线程的实例讲解

小妮浅浅 人气:0
想了解java isInterrupted()判断线程的实例讲解的相关内容吗,小妮浅浅在本文为您仔细讲解java isInterrupted()怎样判断线程的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,isInterrupted(),判断线程,下面大家一起来学习吧。

1、说明

isInterrupted()可以判断当前线程是否被中断,仅仅是对interrupt()标识的一个判断,并不会影响标识发生任何改变(因为调用interrupt()的时候会设置内部的一个叫interrupt flag的标识)。

2、实例

public static void main(String[] args) throws InterruptedException{
    Thread thread = new Thread(()->{
        while (true){}
    });
    thread.start();
    TimeUnit.SECONDS.sleep(1);
    System.out.println("Thread is interrupted :"+thread.isInterrupted());
    thread.interrupt();
    System.out.println("Thread is interrupted :"+thread.isInterrupted());
}

实例扩展补充:

ublic class t12 {
    public static void main(String[] args) {
        try {
            MyThread12 thread = new MyThread12();
            thread.start();
            Thread.sleep(500);
            thread.interrupt();
            System.out.println("是否终止1? =" + thread.interrupted());
            System.out.println("是否终止2? =" + thread.interrupted());
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("-------------end-------------");
    }
}

class MyThread12 extends Thread {
    public void run() {
        for (int i = 0; i < 50000; i++) {
            System.out.println("i = " + i);
        }
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论