zoukankan      html  css  js  c++  java
  • Interrupt

    Interrupt ,给线程发送一个中断信号,如给t1线程发送中断信号,t1.interrupt();

    isInterrupted()  检测线程的中断信号状态 ,返回true的条件是线程是alive的,线程被中断了。如果线程已经结束了not alive,则返回false

    wait和sleep和join这三个方法都会抛出InterruptException检查异常

    /**
     * 
     * 在t1线程里没有sleep,join,wait方法时,给t1线程发送中断信号, t1.interrupt();
     * t1线程的中断状态由false变为true了
     *
     */
    public class Demo11 {
    
        public static void main(String[] args) throws InterruptedException {
    
            Thread t1 = new Thread(() -> {
                System.out.println("t1 is running");
                while (true) {
    
                }
            });
            t1.start();
    
            try {
                Thread.sleep(1_000);
                System.out.println("before "+t1.isInterrupted());
                t1.interrupt();
                System.out.println("after "+t1.isInterrupted());
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
    
        }
    }
    /**
     * 
     * 在t1线程里没有sleep,join,wait方法时,给t1线程发送中断信号, t1.interrupt();
     * t1线程的中断状态由false变为true了
     * 在main线程用t1.isInterrupted()检测t1线程的中断状态,
     * main线程sleep一会儿,t1线程这时候break跳出循环了,t1线程结束了,那么用t1.isInterrupted()检查的结果一定是false
     * main线程去掉sleep,相当于给线程t1发送中断信号后,立即使用t1.isInterrupted()检查状态,结果一定是true
     *
     */
    public class Demo12 {
    
        public static void main(String[] args) throws InterruptedException {
    
            Thread t1 = new Thread(() -> {
                System.out.println("t1 is running");
                while (true) {
                   // System.out.println("#######");
                    //如果检测到中断信号变为true了,跳出循环,t1线程变为ternated了,t1线程结束了
                    if(Thread.currentThread().isInterrupted()) {
                        break;
                    }
                }
            });
            t1.start();
    
            try {
                Thread.sleep(1_000);
                System.out.println("before "+t1.isInterrupted()); //false
                t1.interrupt();
               // Thread.sleep(100);
                System.out.println("after "+t1.isInterrupted() + ",  alive =  " + t1.isAlive());  //false
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
    
        }
    }

    当线程中使用了wait方法

    /**
     * 
     *当线程中有sleep,wait,join等方法时,采用 t.interrupt();的方式打断线程,因为这些方法sleep,wait,join
     *会捕获检查异常InterruptedException,并且捕获异常时候,中断信号被clear,即isInterrupted()返回false,
     *
     *
     */
    public class Demo13 {
    
        private static final Object MONITOR  =  new Object();
        
        public static void main(String[] args) {
            
              Thread t = new Thread() {
                    @Override
                    public void run() {
                        while (true) {
                            synchronized (MONITOR) {
                                System.out.println("=========>Before");
                                try {
                                    MONITOR.wait();
                                    System.out.println("=========>Wait");
                                } catch (InterruptedException e) {
                                    System.out.println("###"+isInterrupted());
                                    e.printStackTrace();
                                }
                                System.out.println("=========>After");
                            }
                        }
                    }
                };
                t.start();
                
                try {
                    Thread.sleep(1_000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                t.interrupt();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("^^^"+t.isInterrupted());
                
        }
    }

    当线程中使用sleep方法

    /**
     * 
     * 捕获InterruptedException后,会将中断信号状态clear,即中断信号为false
     *
     */
    public class Demo14 {
    
        public static void main(String[] args) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            System.out.println("###" + isInterrupted());
                            e.printStackTrace();
                        }
                    }
                }
            };
            t.start();
    
            try {
                Thread.sleep(1_000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
    
            t.interrupt();
            //这里休眠一段时间是为了让t线程在捕获异常后,将中断信号状态clear掉,即t.isInterrupted()会返回false
            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            System.out.println("^^^" + t.isInterrupted() + " alive : " + t.isAlive());
        }
    }

    当线程中使用join方法

    /**
     * 
     * t.join();抛出InterruptedException的条件
     * 当前线程是main,如果打断了main线程,t.join(),方法才抛出异常 ,同样main线程 的中断信号状态也被clear了,
     */
    public class Demo15 {
        public static void main(String[] args) {
    
            Thread t = new Thread() {
                @Override
                public void run() {
                    while (true) {
    
                    }
                }
            };
            t.start();
    
            // 获取当前线程
            Thread main = Thread.currentThread();
            Thread t2 = new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    main.interrupt();
                    System.out.println("打断main线程了");
                }
            };
            t2.start();
    
            try {
               // 这段代码是指t线程执行完后,再继续执行main线程,如果这时候打断main线程,那么就会抛出异常
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    软件工程个人作业01
    阅读计划及浅读问题
    引言作业1
    多态和异常分析课后
    大道至简七八章阅读
    接口与继承 课后实践
    构建执法阅读笔记02
    冲刺第五天
    学习进度条七
    冲刺第四天
  • 原文地址:https://www.cnblogs.com/moris5013/p/10704808.html
Copyright © 2011-2022 走看看