synchronized
互斥 可重入
案例
public synchronized void synchronizedMethod(){
System.out.println("synchronized 非静态方法锁");
}
public synchronized void synchronizedMethod();
descriptor: ()V
**flags: ACC_PUBLIC, ACC_SYNCHRONIZED**
Code:
stack=2, locals=1, args_size=1
0: getstatic #2
3: ldc #3
5: invokevirtual #4
8: return
LineNumberTable:
line 7: 0
line 8: 8
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 this Lcom/javabase/lock/sync/SynchronizedDemo;
public synchronized static void synchronizedStaticMethod(){
System.out.println("synchronized 静态方法锁");
}
public static synchronized void synchronizedStaticMethod();
descriptor: ()V
**flags: ACC_PUBLIC, ACC_STATIC, ACC_SYNCHRONIZED**
Code:
stack=2, locals=0, args_size=0
0: getstatic #2
3: ldc #5
5: invokevirtual #4
8: return
LineNumberTable:
line 11: 0
line 12: 8
public void synchronizedObjectStatic(){
synchronized(this){
System.out.println("synchronized 块锁--对象的方式");
}
}
public void synchronizedObjectStatic();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=2, locals=3, args_size=1
0: aload_0
1: dup
2: astore_1
**3: monitorenter**
4: getstatic #2
7: ldc #6
9: invokevirtual #4
12: aload_1
**13: monitorexit**
14: goto 22
17: astore_2
18: aload_1
**19: monitorexit**
20: aload_2
21: athrow
22: return
Exception table:
from to target type
4 14 17 any
17 20 17 any
LineNumberTable:
line 24: 0
line 25: 4
line 26: 12
line 27: 22
LocalVariableTable:
Start Length Slot Name Signature
0 23 0 this Lcom/javabase/lock/sync/SynchronizedDemo;
StackMapTable: number_of_entries = 2
frame_type = 255
offset_delta = 17
locals = [ class com/javabase/lock/sync/SynchronizedDemo, class java/lang/Object ]
stack = [ class java/lang/Throwable ]
frame_type = 250
offset_delta = 4
public static void synchronizedClassStatic(){
synchronized(SynchronizedDemo.class){
System.out.println("synchronized 块锁--类的方式");
}
}
public static void synchronizedClassStatic();
descriptor: ()V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=0
0: ldc #7
2: dup
3: astore_0
**4: monitorenter**
5: getstatic #2
8: ldc #8
10: invokevirtual #4
13: aload_0
**14: monitorexit**
15: goto 23
18: astore_1
19: aload_0
**20: monitorexit**
21: aload_1
22: athrow
23: return
Exception table:
from to target type
5 15 18 any
18 21 18 any
LineNumberTable:
line 33: 0
line 34: 5
line 35: 13
line 36: 23
StackMapTable: number_of_entries = 2
frame_type = 255
offset_delta = 18
locals = [ class java/lang/Object ]
stack = [ class java/lang/Throwable ]
frame_type = 250
offset_delta = 4