验证子类和父类是同一把锁

知识前提:

    synchronized是可重入锁

     一个类有两种锁,Class锁和对象锁,static方法使用的是Class锁,并且是所有static方法唯一的一把锁。对象锁是每new出来一个对象就会产生一个锁,并且这个对象的所有非静态方法共用这一个锁,不同方法的非静态方法使用的锁不是同一把锁。

验证代码:

package com.gui.thread;

import java.util.concurrent.TimeUnit;

/**
 * ParentAndSonSynchronized
 *
 * @author guigu
 * @QQ 1170258867
 * @date 2020/5/11
 * @description 验证父类和子类是否为同一把锁
 * 难点,Synchronized是可重入锁
 */
public class ParentAndSonSynchronized {

    public static void main(String[] args) {
        Son son = new Son();
        new Thread(()->{
            try {
                son.test1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                son.test2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

class Parent{
    public synchronized void test1() throws InterruptedException {
        System.out.println("这个是父类方法,抱着锁睡");
        TimeUnit.SECONDS.sleep(5);
        System.out.println("父类睡醒了");
    }
    public synchronized void test2() throws InterruptedException {
        System.out.println("用于子类继承");
    }
}
class Son extends Parent{

    @Override
    public synchronized void test2() throws InterruptedException {
        super.test2();
        TimeUnit.SECONDS.sleep(2);
        System.out.println("子类方法运行了");
    }
}

还没完:

   对于静态方法,谁的锁就是谁的锁,就是Class锁不共用

package com.gui.thread;

import java.util.concurrent.TimeUnit;

/**
 * ParentAndSonSynchronized
 *
 * @author guigu
 * @QQ 1170258867
 * @date 2020/5/11
 * @description 验证父类和子类是否为同一把锁
 * 难点,Synchronized是可重入锁
 */
public class ParentAndSonSynchronizedUseStatic {

    public static void main(String[] args) {
        new Thread(()->{
            try {
               Son1.test1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                Son1.test2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

class Parent1{
    public static synchronized void test1() throws InterruptedException {
        System.out.println("这个是父类方法,抱着锁睡");
        TimeUnit.SECONDS.sleep(5);
        System.out.println("父类睡醒了");
    }
    public static synchronized void test2() throws InterruptedException {
        System.out.println("用于子类继承");
    }
}

class Son1 extends Parent1{
//    public static synchronized void test1() throws InterruptedException {
//        TimeUnit.SECONDS.sleep(2);
//        System.out.println("子类方法运行了");
//    }

    public static synchronized void test2() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("子类方法运行了");
    }
}
package com.gui.thread;

import java.util.concurrent.TimeUnit;

/**
 * ParentAndSonSynchronized
 *
 * @author guigu
 * @QQ 1170258867
 * @date 2020/5/11
 * @description 验证父类和子类是否为同一把锁
 * 难点,Synchronized是可重入锁
 */
public class ParentAndSonSynchronizedUseStatic {

    public static void main(String[] args) {
        new Thread(()->{
            try {
               Parent1.test1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                Son1.test1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

class Parent1{
    public static synchronized void test1() throws InterruptedException {
        System.out.println("这个是父类方法,抱着锁睡");
        TimeUnit.SECONDS.sleep(5);
        System.out.println("父类睡醒了");
    }
    public static synchronized void test2() throws InterruptedException {
        System.out.println("用于子类继承");
    }
}

class Son1 extends Parent1{
    public static synchronized void test1() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("子类方法运行了");
    }

    public static synchronized void test2() throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("子类方法运行了");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值