java-concurrent:sychronized
1 对于普通同步方法,锁是当前实例对象
2 对于静态同步方法,锁是当前类Class对象
3 对于同步方法块,锁是Synchronized括号里配置的对象
4 JVM基于进入和退出Monitor对象来显示方法同步和代码块同步,但两者的实现细节不一样。代码块同步是使用monitorenter和monitorexit指令实现的,而方法同步是使用另外一种方式实现的。
monitorenter指令是在编译后插入到同步代码块的开始位置,而monitorexit是插入到方法的结束处和异常处,JVM保证每个monitorenter必须有对应的monitorexit与之配对。
C:\Users\morga>javap -c -verbose D:\gitee\projectx\ml-common\target\classes\com\xiaosq\common\MainTestSynchronized.class Classfile /D:/gitee/projectx/ml-common/target/classes/com/xiaosq/common/MainTestSynchronized.class Last modified 2019-9-10; size 841 bytes MD5 checksum 3d25a1b989989b9b30f9e22feee77676 Compiled from "MainTestSynchronized.java" public class com.xiaosq.common.MainTestSynchronized minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #8.#31 // java/lang/Object."<init>":()V #2 = Fieldref #4.#32 // com/xiaosq/common/MainTestSynchronized.aa:I #3 = Fieldref #4.#33 // com/xiaosq/common/MainTestSynchronized.bb:I #4 = Class #34 // com/xiaosq/common/MainTestSynchronized #5 = Methodref #4.#31 // com/xiaosq/common/MainTestSynchronized."<init>":()V #6 = Methodref #4.#35 // com/xiaosq/common/MainTestSynchronized.first:()V #7 = Methodref #4.#36 // com/xiaosq/common/MainTestSynchronized.second:()V #8 = Class #37 // java/lang/Object #9 = Utf8 aa #10 = Utf8 I #11 = Utf8 bb #12 = Utf8 <init> #13 = Utf8 ()V #14 = Utf8 Code #15 = Utf8 LineNumberTable #16 = Utf8 LocalVariableTable #17 = Utf8 this #18 = Utf8 Lcom/xiaosq/common/MainTestSynchronized; #19 = Utf8 first #20 = Utf8 second #21 = Utf8 StackMapTable #22 = Class #34 // com/xiaosq/common/MainTestSynchronized #23 = Class #37 // java/lang/Object #24 = Class #38 // java/lang/Throwable #25 = Utf8 main #26 = Utf8 ([Ljava/lang/String;)V #27 = Utf8 args #28 = Utf8 [Ljava/lang/String; #29 = Utf8 SourceFile #30 = Utf8 MainTestSynchronized.java #31 = NameAndType #12:#13 // "<init>":()V #32 = NameAndType #9:#10 // aa:I #33 = NameAndType #11:#10 // bb:I #34 = Utf8 com/xiaosq/common/MainTestSynchronized #35 = NameAndType #19:#13 // first:()V #36 = NameAndType #20:#13 // second:()V #37 = Utf8 java/lang/Object #38 = Utf8 java/lang/Throwable { public com.xiaosq.common.MainTestSynchronized(); descriptor: ()V flags: ACC_PUBLIC Code: stack=2, locals=1, args_size=1 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: aload_0 5: iconst_0 6: putfield #2 // Field aa:I 9: aload_0 10: iconst_0 11: putfield #3 // Field bb:I 14: return LineNumberTable: line 8: 0 line 10: 4 line 12: 9 LocalVariableTable: Start Length Slot Name Signature 0 15 0 this Lcom/xiaosq/common/MainTestSynchronized; public synchronized void first(); descriptor: ()V flags: ACC_PUBLIC, ACC_SYNCHRONIZED Code: stack=3, locals=1, args_size=1 0: aload_0 1: aload_0 2: getfield #2 // Field aa:I 5: iconst_1 6: iadd 7: putfield #2 // Field aa:I 10: return LineNumberTable: line 15: 0 line 16: 10 LocalVariableTable: Start Length Slot Name Signature 0 11 0 this Lcom/xiaosq/common/MainTestSynchronized; public void second(); descriptor: ()V flags: ACC_PUBLIC Code: stack=3, locals=3, args_size=1 0: aload_0 1: dup 2: astore_1 3: monitorenter 4: aload_0 5: aload_0 6: getfield #3 // Field bb:I 9: iconst_1 10: iadd 11: putfield #3 // Field bb:I 14: aload_1 15: monitorexit 16: goto 24 19: astore_2 20: aload_1 21: monitorexit 22: aload_2 23: athrow 24: return Exception table: from to target type 4 16 19 any 19 22 19 any LineNumberTable: line 19: 0 line 20: 4 line 21: 14 line 22: 24 LocalVariableTable: Start Length Slot Name Signature 0 25 0 this Lcom/xiaosq/common/MainTestSynchronized; StackMapTable: number_of_entries = 2 frame_type = 255 /* full_frame */ offset_delta = 19 locals = [ class com/xiaosq/common/MainTestSynchronized, class java/lang/Object ] stack = [ class java/lang/Throwable ] frame_type = 250 /* chop */ offset_delta = 4 public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=1 0: new #4 // class com/xiaosq/common/MainTestSynchronized 3: dup 4: invokespecial #5 // Method "<init>":()V 7: astore_1 8: aload_1 9: invokevirtual #6 // Method first:()V 12: aload_1 13: invokevirtual #7 // Method second:()V 16: return LineNumberTable: line 26: 0 line 27: 8 line 29: 12 line 30: 16 LocalVariableTable: Start Length Slot Name Signature 0 17 0 args [Ljava/lang/String; 8 9 1 main Lcom/xiaosq/common/MainTestSynchronized; } SourceFile: "MainTestSynchronized.java"
java-concurrent/sychronized.txt · Last modified: 2019/09/10 08:39 by 127.0.0.1