为什么监视器锁(ObjectMonitor)、Lock 要设计成可重入的目的主要有两个
1、防止死锁
2、面向对象更好的封装代码
先看一段代码
@Slf4j public class SynchronizedDemo { private static final Object lock = new Object(); public static void main(String[] args) { synchronized (lock) { log.info("enjoy eat"); synchronized (lock) { log.info("enjoy sleep"); } } } }
上述代码中,某个线程第一次成功获取到监视器锁之后,如果 monitor 是不可重入的,当该线程尝试去获取第二个锁的时候阻塞了,那么整个线程就会进入死锁状态,无法继续向下执行了
再看一段代码
@Slf4j public class SynchronizedDemo { private static final Object lock = new Object(); public static void main(String[] args) { synchronized (lock) { log.info("enjoy eat"); synchronized (lock) { log.info("enjoy sleep"); synchronized (lock){ log.info("do something"); } } } } }
上述代码只嵌套了三层锁对象,如果需要 10 次加锁呢,岂不是要写 10 层 synchronized(lock),很不优雅,也不好维护,并且不符合面向对象的编程思想
上述代码可以改造成如下,符合面向对象的编程思想
@Slf4j public class SynchronizedDemo { public static void main(String[] args) { LockObject lock = new LockObject(); lock.eat(); } } @Slf4j class LockObject { public synchronized void eat() { log.info("enjoy eat"); sleep(); } public synchronized void sleep() { log.info("enjoy sleep"); doSomething(); } public synchronized void doSomething() { log.info("do something"); } }
标签:info,enjoy,log,synchronized,Lock,成可,lock,public From: https://www.cnblogs.com/xiaomaomao/p/17410051.html