zoukankan      html  css  js  c++  java
  • 关于锁的8个问题

    问题一:下面代码先执行打电话还是发短信      发短信

    public class A {
        public static void main(String[] args) throws InterruptedException {
            Phone phone = new Phone();
    
            // 锁的存在
            new Thread(()->{
                try {
                    phone.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
            TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone.call(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    class Phone {
    
        // synchronized锁的对象是方法的调用者
        // 两个方法用的是同一个锁,谁先拿到谁执行
        public  synchronized void sendmsg() throws InterruptedException {
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public synchronized void call() {
            System.out.println("打电话");
        }
    
    }

    问题二---如果变为两个对象,下面代码先执行打电话还是发短信?      打电话

    public class A {
        public static void main(String[] args) throws InterruptedException {
            Phone phone1 = new Phone();
            Phone phone2 = new Phone();
            
    
            // 锁的存在
            new Thread(()->{
                try {
                    phone1.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
            TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone2.call(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    class Phone {
    
        // synchronized锁的对象是方法的调用者
        // 两个方法用的是同一个锁,谁先拿到谁执行
        public  synchronized void sendmsg() throws InterruptedException {
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public synchronized void call() {
            System.out.println("打电话");
        }
    
    }

    问题三---增加普通方法,先打印发短信还是hello?

    public class B {
        public static void main(String[] args) throws InterruptedException {
            Phone2 phone = new Phone2();
    
            new Thread(()->{
                try {
                    phone.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
             TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone.hello(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    class Phone2 {
    
        // synchronized锁的对象是方法的调用者
        // 两个方法用的是同一个锁,谁先拿到谁执行
        public  synchronized void sendmsg() throws InterruptedException {
    
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public synchronized void call() {
            System.out.println("打电话");
        }
        // 这里没有锁,不是同步方法,不受锁的影响
        public  void hello() {
            System.out.println("hello");
        }
    
    }

     问题四---用不同对象去调用发短信和hello方法,执行结果?

    public class B {
        public static void main(String[] args) throws InterruptedException {
            Phone2 phone1 = new Phone2();
            Phone2 phone2 = new Phone2();
    
            new Thread(()->{
                try {
                    phone1.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
             TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone2.hello(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    class Phone2 {
    
        public  synchronized void sendmsg() throws InterruptedException {
    
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public synchronized void call() {
            System.out.println("打电话");
        }
        // 这里没有锁,不是同步方法,不受锁的影响
        public  void hello() {
            System.out.println("hello");
        }
    
    }

     解释同上

    问题5---当加了static关键字之后,执行结果是怎样的?

    public class C {
        public static void main(String[] args) throws InterruptedException {
            Phone3 phone = new Phone3();
            new Thread(()->{
                try {
                    phone.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
            TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone.call(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    // phone3只有唯一的一个class对象 phone3.class(全局唯一)
    class Phone3 {
    
        // synchronized锁的对象是方法的调用者
        // 两个方法用的是同一个锁,谁先拿到谁执行
        // static 静态方法,类一加载就有了,class 模板
        // 此时锁的就是class
        public  static synchronized void sendmsg() throws InterruptedException {
    
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public static synchronized void call() {
            System.out.println("打电话");
        }
    }

    问题6---当加了static关键字来修饰之后,用两个不同对象来调用,执行结果又是怎样?

    public class C {
        public static void main(String[] args) throws InterruptedException {
            // 两个对象,但此时锁的是class,不关对象事。
            // 两个对象的class类模板只有一个
            Phone3 phone1 = new Phone3();
            Phone3 phone2 = new Phone3();
    
    
            new Thread(()->{
                try {
                    phone1.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
            TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone2.call(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    // phone3只有唯一的一个class对象 phone3.class(全局唯一)
    class Phone3 {
    
        // synchronized锁的对象是方法的调用者
        // 两个方法用的是同一个锁,谁先拿到谁执行
        // static 静态方法,类一加载就有了,class 模板
        // 此时锁的就是class
        public  static synchronized void sendmsg() throws InterruptedException {
    
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public static synchronized void call() {
            System.out.println("打电话");
        }
    }

    问题7--普通同步方法和静态同步方法放一起,执行顺序是怎样的?

    public class D {
        public static void main(String[] args) throws InterruptedException {
    
            Phone4 phone = new Phone4();
    
            new Thread(()->{
                try {
                    phone.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
            TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone.call(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    // phone3只有唯一的一个class对象 phone3.class(全局唯一)
    class Phone4 {
    
    
        // 静态同步方法
        public  static synchronized void sendmsg() throws InterruptedException {
    
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        // 普通同步方法
        public  synchronized void call() {
            System.out.println("打电话");
        }
    
    
    }

     分析:

     问题8:两个不同对象来调用,结果又是怎样?

    public class D {
        public static void main(String[] args) throws InterruptedException {
    
            Phone4 phone1 = new Phone4();
            Phone4 phone2 = new Phone4();
    
            new Thread(()->{
                try {
                    phone1.sendmsg();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"A").start();
            // 休息1s
            TimeUnit.SECONDS.sleep(1);
    
            new Thread(()->{ phone2.call(); },"B").start();
    
        }
    }
    @SuppressWarnings("all")
    // phone3只有唯一的一个class对象 phone3.class(全局唯一)
    class Phone4 {
    
    
        // 静态同步方法
        public  static synchronized void sendmsg() throws InterruptedException {
    
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        // 普通同步方法
        public  synchronized void call() {
            System.out.println("打电话");
        }
    
    
    }

    结果:

  • 相关阅读:
    60.django session缓存配置
    Jmeter学习笔记-Jmeter目录文件
    Jmeter用户手册目录
    Jmeter学习笔记-jmeter执行结束报错:The JVM should have exitted but did not
    PPT-分割图片
    bug记录-权限问题,比如说默认搜索条件为子系统,则在该子系统下面的接口模版,在搜索条件下也应该有权限的限制
    bug记录-弹出框,刚配置的时候记录少,当记录过多时,容易出现高度过高,所以要对高度进行限制,以及设置滚动条
    Jmeter学习笔记-20181224安装软件路径及linux安装方法
    bug记录-列表头部缺失,点开页面,列表头部存在,然后点击搜索后,某个枚举没有值,搜出来没有列表头部,换成其他有值的列表头部,列表头部不显示了
    视频录制与剪辑备忘记录
  • 原文地址:https://www.cnblogs.com/YXBLOGXYY/p/14862720.html
Copyright © 2011-2022 走看看