interrupt(),interrupted(),isInterrupted()

BEGIN:

package test;

class ThreadInterrupt implements Runnable{

    @Override
    public void run() {
        System.out.println("run----子线程正在运行");
        try {
            Thread.sleep(20000);//子线程休息20秒,等待main线程来终端
        } catch (InterruptedException e) {
            System.out.println("run-----子线程在休息的时候被中断");
            /*   
             * 输出false,原因为:
             * if any thread has interrupted the current thread. 
             * The interrupted status of the current thread is cleared when this exception is thrown.
             */
            System.out.println(Thread.currentThread().isInterrupted());
            return;//如果没有return,则即使线程中断也不会立即返回,他还会继续运行下面的代码(中断现成的最好状态)
        }
        System.out.println("run-------子线程运行结束");
    }
}

public class Main3 {
    /*
     * 子线程应该休息20秒的,但是在休到5秒的时候,被interrupt()方法打断了,被打断之后,立刻抛出InterruptedException异常
     * 输出:
        run----子线程正在运行
        main ----即将中断子线程
        main-----main线程运行结束结束
        run-----子线程在休息的时候被中断
     */
    public static void main(String[] args) {
        //开启一个线程
        ThreadInterrupt runnable=new ThreadInterrupt();
        Thread t=new Thread(runnable);
        t.start();
        try {
            Thread.sleep(5000);//使main线程休息会,让子线程有时间运行会。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //中断子线程
        System.out.println("main ----即将中断子线程");
        t.interrupt();
        System.out.println("main-----main线程运行结束结束");
    }
    /*
     * 当为flag为true的时候,当前线程被中断,但是还是可以继续往下执行的,当遇到Thread.sleep(4000)方法的时候,会立刻抛出InterruptedException异常、
     * 输出:
        当前线程即将sleep的前一条语句
        程序被中断
        程序运行的时间:0
     * 当为flag为false的时候,一切如常啊
     * 输出:
           当前线程即将sleep的前一条语句
        程序没有被中断
        程序运行的时间:4000
     */
    private static boolean flag;
    public static void main(String[] args) {
        flag=false;
        if(flag){
            Thread.currentThread().interrupt();//中断当前线程
        }
        long curTime=System.currentTimeMillis();
        try {//当前线程休眠20秒
            System.out.println("当前线程即将sleep的前一条语句");
            Thread.sleep(4000);
            System.out.println("程序没有被中断");
        } catch (InterruptedException e) {
            System.out.println("程序被中断");
        }
        System.out.println("程序运行的时间:"+(System.currentTimeMillis()-curTime));
    }
    
    /*
     * Thread.interrupted()来判断当前线程是否中断
     * 可以使用Thread.interrupted()方法来检查当前线程的是否中断(并隐式重置为false)。又由于它是静态方法,因此不能在特定的线程上使用,
     * 而只能报告调用它的线程的中断状态,如果线程被中断,而且中断状态尚不清楚,那么,这个方法返回true。与isInterrupted()不同,
     * 它将自动重置中断状态为false,第二次调用Thread.interrupted()方法,总是返回false,除非中断了线程。
     * 源码:
        public static boolean interrupted() {
            return currentThread().isInterrupted(true);  静态方法很叼,一定要清楚咯
        }
        private native boolean isInterrupted(boolean ClearInterrupted);
     * 输出结果为:
        A点:Thread.interruped()的结果为false
        B点:Thread.interruped()的结果为true
        C点:Thread.interruped()的结果为false
     */
    public static void main(String[] args) {
        System.out.println("A点:Thread.interruped()的结果为"+Thread.interrupted());
        Thread.currentThread().interrupt(); //线程中断
        System.out.println("B点:Thread.interruped()的结果为"+Thread.interrupted());
        System.out.println("C点:Thread.interruped()的结果为"+Thread.interrupted());
    }
    
    /*
     * isInterrupted()来判断线程是否被中断(不是静态方法,对象调用,判断什么线程都可以啊)(说白了就是简单地查询中断,不会去修改状态,非静态的对象调用的方法就是好)
     * 从结果可以看出,当线程被中断后,Thread.currentThread().isInterrupted()的结果就被一直是ture,与interrupted()不一样,不会重置会false;这一点要区分开
     * 源码:
        public boolean isInterrupted() {
            return isInterrupted(false); 非静态方法不敢叼,就清除咯
        }
        private native boolean isInterrupted(boolean ClearInterrupted);
     * 输出结果为:
        A点: Thread.currentThread().isInterrupted()=false
        B点: Thread.currentThread().isInterrupted()=true
        C点: Thread.currentThread().isInterrupted()=true
     */
    public static void main(String[] args) {
        System.out.println("A点: Thread.currentThread().isInterrupted()="+Thread.currentThread().isInterrupted());
        Thread.currentThread().interrupt(); //线程中断
        System.out.println("B点: Thread.currentThread().isInterrupted()="+Thread.currentThread().isInterrupted());
        System.out.println("C点: Thread.currentThread().isInterrupted()="+Thread.currentThread().isInterrupted());
    }
}

 比较:

用obj.isInterrupted()

/*
 * 输出:
 *  ......
    false  70
    false  71
    false  72
    false  73
    false  74
    false  75
    true  76
    true  77
    true  78
    true  79
    ......
    true  9998
    true  9999
 *  注意:每次从true到false的时间都是不一样的
 */
public class Main2 {  
    public static void main(String... args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    long a = 0;
                    while (true) {
                        // (1)第一次运行到这里的时候,innterrupted状态明显为false,不抛异常
                        // (3)第二次运行到这里的时候,innterrupted状态明显为true,就立刻抛异常了
                        Thread.sleep(1);
                        for (int i = 0; i < 100; i++) {
                            for (int j = 0; j < 100; j++) {
                                //(2) 当a为76的时候,innterrupted状态变为true了,因为是obj.isInterrupted(),不改变innterrupted状态,之后一直运行到a为9999
                                System.out.println(Thread.currentThread().isInterrupted() + "  " +a++);
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    System.out.println("卧槽");
                    e.printStackTrace();
                }
            }
        });
        t.start();
        Thread.sleep(3);
        t.interrupt();
    }  
}

 用Thread.interrupt()

/*
 * 输出:
 *  ......
    false  100
    false  101
    false  102
    false  103
    true  104
    false  105
    false  106
    false  107
    false  108
    .......(一直无限输出)
 *  注意:每次从true到false的时间都是不一样的
 */
public class Main2 {  
    public static void main(String... args) throws InterruptedException {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    long a = 0;
                    while (true) {
                        // (1)第一次运行到这里的时候,innterrupted状态明显为false,不抛异常
                        // (3)第二次运行到这里的时候,innterrupted状态明显为false,不抛异常
                        // (5)innterrupted状态一直未false,不抛异常,进去死循环
                        Thread.sleep(1);
                        for (int i = 0; i < 100; i++) {
                            for (int j = 0; j < 100; j++) {
                                //(2) 当a为76的时候,innterrupted状态变为true了,因为是Thread.interrupted(),改变innterrupted状态了,变成false,之后一直运行到a为99
                                //(4)innterrupted状态一直未false
                                System.out.println(Thread.interrupted() + "  " +a++);
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    System.out.println("卧槽");
                    e.printStackTrace();
                }
            }
        });
        t.start();
        Thread.sleep(3);
        t.interrupt();
    }  
}

END:

猜你喜欢

转载自www.cnblogs.com/ericguoxiaofeng/p/9182352.html