Skip to content

Commit 7dbceff

Browse files
committed
更新
1 parent 2dde883 commit 7dbceff

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

MD/Synchronize.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,53 @@
2121
![](https://ws2.sinaimg.cn/large/006tNc79ly1fn26q2val1j31e80hyq56.jpg)
2222

2323

24+
通过一段代码来演示:
25+
26+
```java
27+
public static void main(String[] args) {
28+
synchronized (Synchronize.class){
29+
System.out.println("Synchronize");
30+
}
31+
}
32+
```
33+
34+
使用 `javap -c Synchronize` 可以查看编译之后的具体信息。
35+
36+
```
37+
public class com.crossoverjie.synchronize.Synchronize {
38+
public com.crossoverjie.synchronize.Synchronize();
39+
Code:
40+
0: aload_0
41+
1: invokespecial #1 // Method java/lang/Object."<init>":()V
42+
4: return
43+
44+
public static void main(java.lang.String[]);
45+
Code:
46+
0: ldc #2 // class com/crossoverjie/synchronize/Synchronize
47+
2: dup
48+
3: astore_1
49+
**4: monitorenter**
50+
5: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
51+
8: ldc #4 // String Synchronize
52+
10: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
53+
13: aload_1
54+
**14: monitorexit**
55+
15: goto 23
56+
18: astore_2
57+
19: aload_1
58+
20: monitorexit
59+
21: aload_2
60+
22: athrow
61+
23: return
62+
Exception table:
63+
from to target type
64+
5 15 18 any
65+
18 21 18 any
66+
}
67+
```
68+
69+
可以看到在同步块的入口和出口分别有 `monitorenter,monitorexit`
70+
指令。
71+
2472
> Synchronize 关键字也支持重入。
2573

0 commit comments

Comments
 (0)