zoukankan      html  css  js  c++  java
  • synchronized父子类对象锁重入

    synchronize必须是重入锁,为什么呢? 就是因为父子类不会破坏继承基础思想:

    父子类对象锁重入的意思是子类的同步方法调用了父类的同步方法同样不需要再去竞争锁,因为该线程已经拥有了该对象的锁,子类对象继承了父类的方法。

    我们看下代码:

    父类:

    package com.ck.thread;
     
     
    public class Father {
     
     
        public synchronized void eat() {
     
            System.out.println("Father:吃饭");
     
            xiwan();
     
            System.out.println("Father:吃饭整个过程结束");
     
        }
       
     
        public synchronized void xiwan() {
     
            System.out.println("Father:洗碗");
     
        }
     
     
    }

    子类中的洗手吃饭方法调用了父类的吃饭方法:

    package com.ck.thread;
     
     
    public class Son extends Father{
       
     
        public synchronized void xishouEat() {
     
            System.out.println("son : 洗手");
     
            /**
             * 吃饭之前先洗手,洗完手再吃饭
             */
     
            eat();
     
        }
     
    }

    然后我们修改下吃饭的线程类:

    package com.ck.thread;
     
     
     
    public class EatThread extends Thread{
     
       
        private Son son;
     
     
        public EatThread(Son son) {
     
            super();
     
            this.son = son;
     
        }
     
     
        public Son getSon() {
     
            return son;
     
        }
     
     
        public void setFather(Son son) {
     
            this.son = son;
     
        }
       
     
        @Override
     
        public void run() {
     
            son.xishouEat();
     
        }
     
     
    }

    执行主方法:

    package com.ck.thread;
     
     
    public class MainThread {
     
     
        public static void main(String[] args) throws InterruptedException {
     
            Son son = new Son();
     
            EatThread eatThread = new EatThread(son);
     
            eatThread.start();
     
        }
     
     
    }

    执行结果:

    son : 洗手
     
    Father:吃饭
     
    Father:洗碗
     
    Father:吃饭整个过程结束

    总结:通过上面例子我们可以看到 xishouEat 方法调用父类eat方法的时候,可以直接调用,不需要等待,这就是synchronized 父子类锁重入 ,因为子类继承了父类实际上就是拥有了父类的public方法,所有父类的public 方法也属于子类对象。同一个对象同步方法调用就是我们上一篇讲的内容,同一个对象拥有了同一把锁,不需要竞争。

  • 相关阅读:
    C++中new申请动态数组
    iOS 9之3D Touch功能
    在xcode找不到发布证书
    iOS 企业版 打包
    iOS证书详解--转载
    Failed to load the JNI shared library jvm.dl
    Xcode7.3打包ipa文件 报错和解决
    更新mac系统和更新到Xcode7.3版本出现的: cannot create __weak reference in file using manual reference counting
    Sun jdk, Openjdk, Icedtea jdk关系
    terminal color
  • 原文地址:https://www.cnblogs.com/47Gamer/p/15022337.html
Copyright © 2011-2022 走看看