synchronize底层原理
好酷屋教程网小编为您收集和整理了synchronize底层原理的相关教程:synchronize底层原理是什么?我们先通过反编译下面的代码来看看Synchronized是如何实现对代码块进行同步的:1packagecom.paddx.test.concurrent;23pu
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 }
反编译结果:
关于这两条指令的作用,我们直接参考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 }
反编译结果:
从反编译的结果来看,方法的同步并没有通过指令monitorenter和monitorexit来完成(理论上其实也可以通过这两条指令来实现),不过相对于普通方法,其常量池中多了ACC_SYNCHRONIZED标示符。JVM就是根据该标示符来实现方法的同步的:当方法调用时,调用指令将会检查方法的 ACC_SYNCHRONIZED 访问标志是否被设置,如果设置了,执行线程将先获取monitor,获取成功之后才能执行方法体,方法执行完后再释放monitor。在方法执行期间,其他任何线程都无法再获得同一个monitor对象。 其实本质上没有区别,只是方法的同步是一种隐式的方式来实现,无需通过字节码来完成。
以上就是好酷屋教程网小编为您收集和整理的synchronize底层原理相关内容,如果对您有帮助,请帮忙分享这篇文章^_^
本文来源: https://www.haoku5.com/shuma/64180b79af59f4a0bb020493.html
相关推荐
热门专题
平板电脑搜狗键盘怎么移动图片(怎么移动搜狗悬浮键盘)
1.怎么移动搜狗悬浮键盘3当出现键盘悬浮的情况,这是因为不小心开启了悬浮键盘的功能,首先在某个聊天界面点击输入框打开键盘。2/3点击键盘的左上角按钮,该按钮图案可能是键盘图案、工具箱图案、搜狗标志图电脑出现选择一个选项疑难解答(电脑出现只有一个选择)
1.电脑出现只有一个选择我的电脑右下角的选择输入法的图标没有了?怎么把它调出来?我的电脑右下角的选择输入法的图标没有了,怎么把它调出来,进入控制面板设置一下就可以了。如图示,在控制面板中进入区域和语电脑连接网络受限制是什么原因(电脑联网显示网络受限)
1.电脑联网显示网络受限如提示网络连接受限制或无连接,可按以下方法操作:1、打开电脑“控制面板”,点击“网络连接”,选择本地连接,右键点击本地连接图标后选“属性”,在“常规”选项卡中双击“Inter三环耳机插电脑(三接头耳机怎么插电脑)
1.三接头耳机怎么插电脑如图所示,电脑耳机插口有两个,每个插孔下面有标识。比如,粉色的下面是个话筒话标志,浅绿色的是耳机🎧,有的电脑上有三个插孔,也就是在中间位置有一个混音插孔,一般是黑色的,就是电脑全屏截图怎么弄(电脑上如何全屏截图)
1.电脑上如何全屏截图 方法一:直接使用键盘中的PrintScrSysrq键实现全屏截图PrintScreenSysRq截图键 使用方法非常简单,只需要按下该键后即可完成全屏截图,然后我们苹果电脑键盘电源(苹果电脑键盘电源灯闪烁是什么意思)
1.苹果电脑键盘电源灯闪烁是什么意思那可能是灯的问题,我的Mac键盘灯是白色的,你去售后问问呗。如果不是键盘脏的话,目测只能是灯的问题了吧,盖膜的应该没什么影响2.苹果电脑键盘指示灯一直闪如果您天天飞车电脑壁纸(天天飞车电脑壁纸超清)
1.天天飞车电脑壁纸超清汉字中隔壁的壁字是一个常用字,是墙体和陡峭的山涯、某些物体的表面,可以可以组成的词语有:1、墙壁,一堵墙体的表面。2、戈壁,戈壁滩寸草不生,全是沙石。3、壁画,是在墙壁、山涯电脑怎么抓屏(联想电脑怎么抓屏)
1.联想电脑怎么抓屏截图方法有多种:1、按“PrintScreenSysRq”,(在F12右边)全屏截图,就是抓整个屏幕;2、按Alt+PrintScreenSysRq,截取当前电脑保修期一般多长时间(机械革命电脑保修期一般多长时间)
1.机械革命电脑保修期一般多长时间按国家规定整机保一年,配件三年保修2.机械革命电脑保修期一般多长时间啊第1步,查询电脑过没过保修期的方法是:第2步,1、翻转电脑,查看底部铭牌上的主机编号并索尼s39h连接电脑(索尼手机s39h)
1.索尼手机s39h索尼手机摄像头在中间的型号大概有索尼LT25I、索尼LT26I(LT26II)、索尼L35H、索尼M35H(M36H)、索尼S39H、索尼S50H,或许还有我不清楚的,但是我选出