Synchronized解析——一层一层剥开洋葱的心

Synchronized解析——一层一层剥开洋葱的心 引入:https://juejin.im/post/5d5374076fb9a06ac76da894#heading-9 前言 synchronized,是解决并发情况下数据同步访问问题的一把利刃。那么synchronized的底层原理是什么呢?下面我们来一层一层剥开它的心,就像剥洋葱一样,看个究竟。 Synchronized的使用场景 接下来,我们先剥开synchronized的第一层,反编译其作用的代码块以及方法。 synchronized作用于代码块 public class SynchronizedTest { public void doSth(){ synchronized (SynchronizedTest.class){ System.out.println("test Synchronized" ); } } } 反编译,可得: 由图可得,添加了synchronized关键字的代码块,多了两个指令monitorenter、monitorexit。即JVM使用monitorenter和monitorexit两个指令实现同步,monitorenter、monitorexit又是怎样保证同步的呢?我们等下剥第二层继续探索。 synchronized作用于方法 public synchronized void doSth(){ System.out.println("test Synchronized method" ); } 反编译,可得: 由图可得,添加了synchronized关键字的方法,多了ACC_SYNCHRONIZED标记。即JVM通过在方法访问标识符(flags)中加入ACC_SYNCHRONIZED来实现同步功能。 monitorenter、monitorexit、ACC_SYNCHRONIZED 剥完第一层,反编译synchronized的方法以及代码块,我们已经知道synchronized是通过monitorenter、monitorexit、ACC_SYNCHRONIZED实现同步的,它们三作用都是啥呢?我们接着剥第二层: monitorenter monitorenter指令介绍 1 ———————————————– Each object is associated with a monitor. A monitor is locked if and only if it has an owner. The thread that executes monitorenter attempts to gain ownership of the monitor associated with objectref, as follows: