monitor锁

image.png
image.png

字节码指令

  1. public class SynchronizedTest {
  2. static final Object lock = new Object();
  3. static int counter = 0;
  4. public static void main(String[] args) {
  5. synchronized (lock){
  6. counter++;
  7. }
  8. System.out.println(counter);
  9. }
  10. }
  1. Last modified 2021-4-13; size 848 bytes
  2. MD5 checksum 524a368d98b2102c040f86818c154444
  3. Compiled from "SynchronizedTest.java"
  4. public class org.example.concurrency.test.SynchronizedTest
  5. minor version: 0
  6. major version: 52
  7. flags: ACC_PUBLIC, ACC_SUPER
  8. Constant pool:
  9. #1 = Methodref #6.#30 // java/lang/Object."<init>":()V
  10. #2 = Fieldref #7.#31 // org/example/concurrency/test/SynchronizedTest.lock:Ljava/lang/Object;
  11. #3 = Fieldref #7.#32 // org/example/concurrency/test/SynchronizedTest.counter:I
  12. #4 = Fieldref #33.#34 // java/lang/System.out:Ljava/io/PrintStream;
  13. #5 = Methodref #35.#36 // java/io/PrintStream.println:(I)V
  14. #6 = Class #37 // java/lang/Object
  15. #7 = Class #38 // org/example/concurrency/test/SynchronizedTest
  16. #8 = Utf8 lock
  17. #9 = Utf8 Ljava/lang/Object;
  18. #10 = Utf8 counter
  19. #11 = Utf8 I
  20. #12 = Utf8 <init>
  21. #13 = Utf8 ()V
  22. #14 = Utf8 Code
  23. #15 = Utf8 LineNumberTable
  24. #16 = Utf8 LocalVariableTable
  25. #17 = Utf8 this
  26. #18 = Utf8 Lorg/example/concurrency/test/SynchronizedTest;
  27. #19 = Utf8 main
  28. #20 = Utf8 ([Ljava/lang/String;)V
  29. #21 = Utf8 args
  30. #22 = Utf8 [Ljava/lang/String;
  31. #23 = Utf8 StackMapTable
  32. #24 = Class #22 // "[Ljava/lang/String;"
  33. #25 = Class #37 // java/lang/Object
  34. #26 = Class #39 // java/lang/Throwable
  35. #27 = Utf8 <clinit>
  36. #28 = Utf8 SourceFile
  37. #29 = Utf8 SynchronizedTest.java
  38. #30 = NameAndType #12:#13 // "<init>":()V
  39. #31 = NameAndType #8:#9 // lock:Ljava/lang/Object;
  40. #32 = NameAndType #10:#11 // counter:I
  41. #33 = Class #40 // java/lang/System
  42. #34 = NameAndType #41:#42 // out:Ljava/io/PrintStream;
  43. #35 = Class #43 // java/io/PrintStream
  44. #36 = NameAndType #44:#45 // println:(I)V
  45. #37 = Utf8 java/lang/Object
  46. #38 = Utf8 org/example/concurrency/test/SynchronizedTest
  47. #39 = Utf8 java/lang/Throwable
  48. #40 = Utf8 java/lang/System
  49. #41 = Utf8 out
  50. #42 = Utf8 Ljava/io/PrintStream;
  51. #43 = Utf8 java/io/PrintStream
  52. #44 = Utf8 println
  53. #45 = Utf8 (I)V
  54. {
  55. static final java.lang.Object lock;
  56. descriptor: Ljava/lang/Object;
  57. flags: ACC_STATIC, ACC_FINAL
  58. static int counter;
  59. descriptor: I
  60. flags: ACC_STATIC
  61. public org.example.concurrency.test.SynchronizedTest();
  62. descriptor: ()V
  63. flags: ACC_PUBLIC
  64. Code:
  65. stack=1, locals=1, args_size=1
  66. 0: aload_0
  67. 1: invokespecial #1 // Method java/lang/Object."<init>":()V
  68. 4: return
  69. LineNumberTable:
  70. line 6: 0
  71. LocalVariableTable:
  72. Start Length Slot Name Signature
  73. 0 5 0 this Lorg/example/concurrency/test/SynchronizedTest;
  74. public static void main(java.lang.String[]);
  75. descriptor: ([Ljava/lang/String;)V
  76. flags: ACC_PUBLIC, ACC_STATIC
  77. Code:
  78. stack=2, locals=3, args_size=1
  79. 0: getstatic #2 // Field lock:Ljava/lang/Object;
  80. 3: dup
  81. 4: astore_1
  82. 5: monitorenter
  83. 6: getstatic #3 // Field counter:I
  84. 9: iconst_1
  85. 10: iadd
  86. 11: putstatic #3 // Field counter:I
  87. 14: aload_1
  88. 15: monitorexit
  89. 16: goto 24
  90. 19: astore_2
  91. 20: aload_1
  92. 21: monitorexit
  93. 22: aload_2
  94. 23: athrow
  95. 24: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
  96. 27: getstatic #3 // Field counter:I
  97. 30: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
  98. 33: return
  99. Exception table:
  100. from to target type
  101. 6 16 19 any
  102. 19 22 19 any
  103. LineNumberTable:
  104. line 11: 0
  105. line 12: 6
  106. line 13: 14
  107. line 14: 24
  108. line 15: 33
  109. LocalVariableTable:
  110. Start Length Slot Name Signature
  111. 0 34 0 args [Ljava/lang/String;
  112. StackMapTable: number_of_entries = 2
  113. frame_type = 255 /* full_frame */
  114. offset_delta = 19
  115. locals = [ class "[Ljava/lang/String;", class java/lang/Object ]
  116. stack = [ class java/lang/Throwable ]
  117. frame_type = 250 /* chop */
  118. offset_delta = 4
  119. static {};
  120. descriptor: ()V
  121. flags: ACC_STATIC
  122. Code:
  123. stack=2, locals=0, args_size=0
  124. 0: new #6 // class java/lang/Object
  125. 3: dup
  126. 4: invokespecial #1 // Method java/lang/Object."<init>":()V
  127. 7: putstatic #2 // Field lock:Ljava/lang/Object;
  128. 10: iconst_0
  129. 11: putstatic #3 // Field counter:I
  130. 14: return
  131. LineNumberTable:
  132. line 7: 0
  133. line 8: 10
  134. }
  135. SourceFile: "SynchronizedTest.java"

image.png