云骑士一键装系统-简单一键在线装机工具,一键备份还原功能让系统维护更方便!
BIOS设置 win10教程 win7教程 U盘教程 xp教程 win8教程 电脑故障 电脑资讯 手机教程 软件教程 游戏攻略
当前位置:云骑士 > 使用教程 > 电脑资讯 > synchronize底层原理

synchronize底层原理

文案作者: 骑士
2019-07-26 15:02:01
播放量:

synchronize底层原理是什么?我们先通过反编译下面的代码来看看Synchronized是如何实现对代码块进行同步的:

1 package com.paddx.test.concurrent;

2

3 public class SynchronizedDemo {

4 public void method() {

5 synchronized (this) {

6 System.out.println("Method 1 start");

7 }

8 }

9 }

反编译结果:

synchronize底层原理

关于这两条指令的作用,我们直接参考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的异常的原因。

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

源代码:

1 package com.paddx.test.concurrent;

2

3 public class SynchronizedMethod {

4 public synchronized void method() {

5 System.out.println("Hello World!");

6 }

7 }

反编译结果:

synchronize底层原理(1)

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

如何设置拒接所有来电
图文
如何设置拒接所有来电
在设置拒绝所有来电时,需要打开手机设置或者手机电话,需要进入拦截界面进行操作,以下是详细操作步骤:...
阅读:200 发布时间:2023-02-18
手机异常关机怎么回事
图文
手机异常关机怎么回事
手机电池质量有没有问题如电池鼓包,老化等,需要更换电池;电池和手机之间的接触松动;手机中安装了过多功能相同的软件导致手机总是异常关机;手...
阅读:682 发布时间:2020-04-18
怎么用ps临摹插画
图文
怎么用ps临摹插画
分析插画元素,新建800*600画布,用形状工具和钢笔工具画出山,用形状工具画出高度不一的树,用矩形工具和钢笔工具添加锚点画出房子,用钢笔工具画出...
阅读:526 发布时间:2020-11-16
小学生国庆节海报设计图片制作
图文
小学生国庆节海报设计图片制作
以甲虫设计为例,打开微信,点击搜索,输入甲虫设计,关注公众号,点击邀请函,点击海报制作,点击我的,登录,点击海报,选择更多分类,点击节日...
阅读:200 发布时间:2021-09-07
苹果怎么把时钟在桌面上放大
图文
苹果怎么把时钟在桌面上放大
长按手机的空白区域,点击左上方的加号图标,小组件中选择时钟的组件,选择大号时钟的样式,选择下方的添加小组件选项,长按现原有的时钟应用,点...
阅读:1899 发布时间:2020-11-13
win7麦克风没声音怎么设置
win7麦克风没声音怎么设置
浏览:24228
2020-10-17
电脑反应慢是什么原因
电脑反应慢是什么原因
浏览:10481
2021-07-29
东芝笔记本重装系统教程
东芝笔记本重装系统教程
浏览:13299
2020-06-24
电脑网速慢是什么原因
电脑网速慢是什么原因
浏览:18526
2018-07-23
联想笔记本重装系统教程
联想笔记本重装系统教程
浏览:54161
2021-09-22

客服二维码

邮箱:toplearningteam#gmail.com (请将#换成@)

Copyright 2013-2020 云骑士( https://www.yunqishi.net/ ) 版权所有 All Rights Reserved   鄂ICP备2023010145号-1