• synchronized关键字的可重入性


    /**
    父类
    */
    public class SynchronizedDemo1 implements Runnable {
    @Override
    public void run() {
    try {
    method();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    public void method() throws InterruptedException {
    synchronized(SynchronizedDemo1.class) {
    System.out.println(Thread.currentThread().getName() + " super start");
    Thread.sleep(3000);
    System.out.println(Thread.currentThread().getName() + " super finished");
    }
    }

    }
    //子类,在子类的main方法中调用子类的synchronized方法,在method方法中调用父类的method方法,是可以执行父类的method方法的,
    也就是说对于一个锁对象而言,这是传递过去的,也即在这个线程中这个锁是可以重复利用的,那么如果锁都不是同一个对象的话,自然也就不存在什么同步了(一开始没反应过来)

    class Son extends SynchronizedDemo1{
    public void method() throws InterruptedException {
    synchronized(Son.class) {
    System.out.println(Thread.currentThread().getName() + " son start");
    Thread.sleep(3000);
    super.method();
    System.out.println(Thread.currentThread().getName() + " son finished");
    }
    }
    public static void main(String[] args) {
    Son s=new Son();
    try {
    s.method();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
  • 相关阅读:
    体验极佳的程序
    如何修改文档等系统文件的位置
    Demo
    Spring Boot与检索/ElasticSearch
    Java NIO:NIO概述
    Centos7 配置静态IP并使用xshell远程连接
    宏定义能否被赋值
    Centos7没有ETH0网卡
    Bringing up interface eth0: Device eth0 does not seem to be presen
    Git 常用命令
  • 原文地址:https://www.cnblogs.com/cold-windy/p/11718254.html
Copyright © 2020-2023  润新知