monitor锁
字节码指令
public class SynchronizedTest {
static final Object lock = new Object();
static int counter = 0;
public static void main(String[] args) {
synchronized (lock){
counter++;
}
System.out.println(counter);
}
}
Last modified 2021-4-13; size 848 bytes
MD5 checksum 524a368d98b2102c040f86818c154444
Compiled from "SynchronizedTest.java"
public class org.example.concurrency.test.SynchronizedTest
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#30 // java/lang/Object."<init>":()V
#2 = Fieldref #7.#31 // org/example/concurrency/test/SynchronizedTest.lock:Ljava/lang/Object;
#3 = Fieldref #7.#32 // org/example/concurrency/test/SynchronizedTest.counter:I
#4 = Fieldref #33.#34 // java/lang/System.out:Ljava/io/PrintStream;
#5 = Methodref #35.#36 // java/io/PrintStream.println:(I)V
#6 = Class #37 // java/lang/Object
#7 = Class #38 // org/example/concurrency/test/SynchronizedTest
#8 = Utf8 lock
#9 = Utf8 Ljava/lang/Object;
#10 = Utf8 counter
#11 = Utf8 I
#12 = Utf8 <init>
#13 = Utf8 ()V
#14 = Utf8 Code
#15 = Utf8 LineNumberTable
#16 = Utf8 LocalVariableTable
#17 = Utf8 this
#18 = Utf8 Lorg/example/concurrency/test/SynchronizedTest;
#19 = Utf8 main
#20 = Utf8 ([Ljava/lang/String;)V
#21 = Utf8 args
#22 = Utf8 [Ljava/lang/String;
#23 = Utf8 StackMapTable
#24 = Class #22 // "[Ljava/lang/String;"
#25 = Class #37 // java/lang/Object
#26 = Class #39 // java/lang/Throwable
#27 = Utf8 <clinit>
#28 = Utf8 SourceFile
#29 = Utf8 SynchronizedTest.java
#30 = NameAndType #12:#13 // "<init>":()V
#31 = NameAndType #8:#9 // lock:Ljava/lang/Object;
#32 = NameAndType #10:#11 // counter:I
#33 = Class #40 // java/lang/System
#34 = NameAndType #41:#42 // out:Ljava/io/PrintStream;
#35 = Class #43 // java/io/PrintStream
#36 = NameAndType #44:#45 // println:(I)V
#37 = Utf8 java/lang/Object
#38 = Utf8 org/example/concurrency/test/SynchronizedTest
#39 = Utf8 java/lang/Throwable
#40 = Utf8 java/lang/System
#41 = Utf8 out
#42 = Utf8 Ljava/io/PrintStream;
#43 = Utf8 java/io/PrintStream
#44 = Utf8 println
#45 = Utf8 (I)V
{
static final java.lang.Object lock;
descriptor: Ljava/lang/Object;
flags: ACC_STATIC, ACC_FINAL
static int counter;
descriptor: I
flags: ACC_STATIC
public org.example.concurrency.test.SynchronizedTest();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 6: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lorg/example/concurrency/test/SynchronizedTest;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=1
0: getstatic #2 // Field lock:Ljava/lang/Object;
3: dup
4: astore_1
5: monitorenter
6: getstatic #3 // Field counter:I
9: iconst_1
10: iadd
11: putstatic #3 // Field counter:I
14: aload_1
15: monitorexit
16: goto 24
19: astore_2
20: aload_1
21: monitorexit
22: aload_2
23: athrow
24: getstatic #4 // Field java/lang/System.out:Ljava/io/PrintStream;
27: getstatic #3 // Field counter:I
30: invokevirtual #5 // Method java/io/PrintStream.println:(I)V
33: return
Exception table:
from to target type
6 16 19 any
19 22 19 any
LineNumberTable:
line 11: 0
line 12: 6
line 13: 14
line 14: 24
line 15: 33
LocalVariableTable:
Start Length Slot Name Signature
0 34 0 args [Ljava/lang/String;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 19
locals = [ class "[Ljava/lang/String;", class java/lang/Object ]
stack = [ class java/lang/Throwable ]
frame_type = 250 /* chop */
offset_delta = 4
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=2, locals=0, args_size=0
0: new #6 // class java/lang/Object
3: dup
4: invokespecial #1 // Method java/lang/Object."<init>":()V
7: putstatic #2 // Field lock:Ljava/lang/Object;
10: iconst_0
11: putstatic #3 // Field counter:I
14: return
LineNumberTable:
line 7: 0
line 8: 10
}
SourceFile: "SynchronizedTest.java"