File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments