码迷,mamicode.com
首页 > 编程语言 > 详细

Java 学习笔记之 线程isInterrupted方法

时间:2017-10-14 20:06:38      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:stat   技术分享   i++   star   rac   str   ges   image   end   

线程isInterrupted方法:

 

isInterrupted()是Thread对象的方法,测试线程是否已经中断。

public class ThreadRunMain {
    public static void main(String[] args) {
        testMainIsInterruptThread();
    }

    public static void testMainIsInterruptThread(){
        Thread.currentThread().interrupt();
        System.out.println("First call: " + Thread.currentThread().isInterrupted());
        System.out.println("Second call: " + Thread.currentThread().isInterrupted());
        System.out.println("end!");
    }
}

运行结果:

技术分享

public class IsInterruptThread  extends Thread{
    @Override
    public void run() {
        for (int i = 0; i< 5000000; i++){
            System.out.println("i=" + (i + 1));
        }
    }
}

public class ThreadRunMain {
    public static void main(String[] args) {
        testIsInterruptThread();
    }


    public static void testIsInterruptThread(){
        try {
            IsInterruptThread iit = new IsInterruptThread();
            iit.start();
            Thread.sleep(1000);
            iit.interrupt();
            System.out.println("First call: " + iit.isInterrupted());
            System.out.println("Second call: " + iit.isInterrupted());
        } catch (InterruptedException e) {
            System.out.println("Main catch");
            e.printStackTrace();
        }
        System.out.println("end!");
    }
}

运行结果:

技术分享

 

Java 学习笔记之 线程isInterrupted方法

标签:stat   技术分享   i++   star   rac   str   ges   image   end   

原文地址:http://www.cnblogs.com/AK47Sonic/p/7668117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!