Java中的锁(4) 深入理解Synchronized实现原理

来源:互联网 发布:图形界面软件 编辑:程序博客网 时间:2024/06/03 15:41

Synchronized的基本使用

  Synchronized是Java中解决并发问题的一种最常用的方法,也是最简单的一种方法。Synchronized的作用主要有三个:(1)确保线程互斥的访问同步代码(2)保证共享变量的修改能够及时可见(3)有效解决重排序问题。从语法上讲,Synchronized总共有三种用法:

  (1)对于修饰普通方法,锁是当前实例对象。

  (2)对于修饰静态方法,锁是当前类的Class对象。

  (3)对于修饰代码块,锁是Synchonized括号里配置的对象。


Synchronized 原理

如果对上面的执行结果还有疑问,也先不用急,我们先来了解Synchronized的原理,再回头上面的问题就一目了然了。我们先通过反编译下面的代码来看看Synchronized是如何实现对代码块进行同步的:

3 public class SynchronizedDemo {4     public void method() {5         synchronized (this) {6             System.out.println("Method 1 start");7         }8     }9 }

反编译结果:
这里写图片描述

于这两条指令的作用,我们直接参考JVM规范中描述:

monitorenter :

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:
• If the entry count of the monitor associated with objectref is zero, the thread enters the monitor and sets its entry count to one. The thread is then the owner of the monitor.
• If the thread already owns the monitor associated with objectref, it reenters the monitor, incrementing its entry count.
• If another thread already owns the monitor associated with objectref, the thread blocks until the monitor’s entry count is zero, then tries again to gain ownership.

这段话的大概意思为:

每个对象有一个监视器锁(monitor)。当monitor被占用时就会处于锁定状态,线程执行monitorenter指令时尝试获取monitor的所有权,过程如下:

1、如果monitor的进入数为0,则该线程进入monitor,然后将进入数设置为1,该线程即为monitor的所有者。

2、如果线程已经占有该monitor,只是重新进入,则进入monitor的进入数加1.

3.如果其他线程已经占用了monitor,则该线程进入阻塞状态,直到monitor的进入数为0,再重新尝试获取monitor的所有权。

monitorexit: 

The thread that executes monitorexit must be the owner of the monitor associated with the instance referenced by objectref.
The thread decrements the entry count of the monitor associated with objectref. If as a result the value of the entry count is zero, the thread exits the monitor and is no longer its owner. Other threads that are blocking to enter the monitor are allowed to attempt to do so.

这段话的大概意思为:

执行monitorexit的线程必须是objectref所对应的monitor的所有者。

指令执行时,monitor的进入数减1,如果减1后进入数为0,那线程退出monitor,不再是这个monitor的所有者。其他被这个monitor阻塞的线程可以尝试去获取这个 monitor 的所有权。

通过这两段描述,我们应该能很清楚的看出Synchronized的实现原理,Synchronized的语义底层是通过一个monitor的对象来完成,其实wait/notify等方法也依赖于monitor对象,这就是为什么只有在同步的块或者方法中才能调用wait/notify等方法,否则会抛出java.lang.IllegalMonitorStateException的异常的原因。

我们再来看一下同步方法的反编译结果:

源代码:

package com.paddx.test.concurrent;    public class SynchronizedMethod {         public synchronized void method() {             System.out.println("Hello World!");     } }

反编译结果:
这里写图片描述

  从反编译的结果来看,方法的同步并没有通过指令monitorenter和monitorexit来完成(理论上其实也可以通过这两条指令来实现),不过相对于普通方法,其常量池中多了ACC_SYNCHRONIZED标示符。JVM就是根据该标示符来实现方法的同步的:当方法调用时,调用指令将会检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置,如果设置了,执行线程将先获取monitor,获取成功之后才能执行方法体,方法执行完后再释放monitor。在方法执行期间,其他任何线程都无法再获得同一个monitor对象。 其实本质上没有区别,只是方法的同步是一种隐式的方式来实现,无需通过字节码来完成。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为手机导航gps信号弱怎么办 手机屏玻璃裂了怎么办 oppo音量键坏了怎么办 魅蓝手机屏幕碎了怎么办 手机钢化膜边缘有气泡怎么办 手机贴钢化膜边缘有气泡怎么办 苹果手机钢化膜边缘有气泡怎么办 手机膜里的水泡怎么办 膜贴好了有气泡怎么办 后档玻璃膜气泡怎么办 贴膜里面有灰尘怎么办 贴膜边缘不粘怎么办 贴膜时候的气泡怎么办 贴手机膜起泡了怎么办 手机膜起泡泡了怎么办 手机膜鼓起来了怎么办 贴膜中间有气泡怎么办 新车贴膜有气泡怎么办 贴手机膜进灰尘怎么办 贴手机膜有灰尘怎么办 华为mate10后机壳碎了怎么办 手机屏和壳分开了怎么办 oppoa33手机开不了机怎么办 7p背面刮花了怎么办 oppo手机左右两键失灵怎么办 苹果5s屏幕翘起怎么办 0pp0手机有点卡怎么办 手机用久有点卡怎么办 手机有点卡怎么办魅蓝 苹果6排线接反了怎么办 oppo手机解锁密码忘了怎么办 魅蓝5音量小怎么办 魅蓝5密码忘记了怎么办 魅蓝note3锁定了怎么办 魅族mx5系统铃声没有了怎么办 魅族主板坏了怎么办 魅族mx5螺丝滑丝怎么办 魅族魅蓝e手机被锁了怎么办 魅蓝e被锁机了怎么办 魅蓝3s卡顿怎么办 魅族note5玩王者荣耀卡怎么办