Java????????Synchronized??????????
???????????? ???????[ 2016/4/29 13:41:48 ] ??????????????????? Java
????????Synchronized ???
????????????????н????????????????ü??????????????Synchronized????????????????????????????????????????????????????????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???????3?????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???? ?????????????????????????????????????????????????????????????ɡ?
???????????н??????
?????????Synchronized????????????????????????????ж??????
????1???????2?????
???????method1??method2??????????????????????????????????????????????????????????????????????????????????????????????????monitor????????????????????????method1??method2?????????С?
????2???????3?????
???????test??test2??????????????test??test2??????????????????????method1??method2??????????????????????????????????????????monitor??????????????class????????????????????С?
????3???????4?????
???????????????????????????Synchronized?????????????ж????monitor????????δ?????????????????this????method1??method2??????????????????????????????????????????????????????????????????????????顣
??????????
????Synchronized??Java????????г????????????????????????????????????????????????????????????????????????????????????????????????????????Synchronized????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11