• 02.线程的等待与中断


    public class NotifyDemo {
        //创建资源
        private static volatile Object reA = new Object();
        private static volatile Object reB = new Object();
        //创建线程
        public static void main(String[] args) throws InterruptedException{
            Thread threadA = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //获取reA共享资源的监视器锁
                        synchronized (reA){
                            System.out.println("threadA get reA lock");
                            //获取reB共享资源的监视器锁
                            synchronized (reB){
                                System.out.println("threadA get reB lock");
                                System.out.println("threadA release resourceA lock");
                                //当线程调用共享对象的wait()方法时,当前线程只会释放当前共享对象的锁
                                reA.wait();
                            }
                        }
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            });
            Thread threadB = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        //获取reA共享资源的监视器锁
                        synchronized (reA){
                            System.out.println("threadB get reA lock");
                            //获取reB共享资源的监视器锁
                            synchronized (reB){
                                System.out.println("threadB get reB lock");
                                System.out.println("threadB release resourceA lock");
                                reA.wait();
                            }
                        }
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            });
            threadA.start();
            threadB.start();
            //等待两个线程结束
            threadA.join();
            threadB.join();
    
            //threadA get reA lock
            //threadA get reB lock
            //threadA release resourceA lock
            //threadB get reA lock
        }
    }
    
    /**
     *  threadA 调用共享对象 obj 的 wait()方法后阻塞挂起了自己,然后 主线程在休眠 ls 后中断了 threadA 线程,
     *  中断后 threadA 在 obj.wait()处抛出 java.lang. InterruptedException 异常而返回并终止。
     */
    public class NotifyDemo2 {
        //创建资源
        private static Object obj = new Object();
        //创建线程
        public static void main(String[] args) throws InterruptedException{
            Thread threadA = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //获取reA共享资源的监视器锁
                        synchronized (obj){
                            System.out.println("begin");
                            obj.wait();
                        }
                        System.out.println("begin");
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            });
    
            threadA.start();
            Thread.sleep(1000);
            System.out.println("begin interrupt");
            threadA.interrupt();//中断threadA线程
            System.out.println("end interrupt");
            //begin
            //begin interrupt
            //end interrupt
            //java.lang.InterruptedException
            //	at java.base/java.lang.Object.wait(Native Method)
            //	at java.base/java.lang.Object.wait(Object.java:516)
            //	at com.fly.NotifyDemo2$1.run(NotifyDemo2.java:15)
            //	at java.base/java.lang.Thread.run(Thread.java:844)
        }
    }
    
  • 相关阅读:
    C/C++程序员必备的15个编辑器和集成开发环境
    天猫浏览型应用的CDN静态化架构演变
    实用技巧:如何用 CSS 做到完全垂直居中
    JavaScript 字符串操作:substring, substr, slice
    Hybrid App 开发初探:使用 WebView 装载页面
    引领潮流!最顶尖的24个获奖网页作品
    HTML5编程之旅系列一:HTML5 Geolocation 初探
    JavaScript 秘密花园——对象的使用和属性操作
    提高效率!15款最好的 Bug 跟踪应用程序
    最常用的8款 PHP 调试工具,你用过吗?
  • 原文地址:https://www.cnblogs.com/fly-book/p/11358423.html
Copyright © 2020-2023  润新知