站点图标 度崩网-几度崩溃

synchronized关键字的作用是什么?

Java 中关键字synchronized表示只有一个线程可以获取作用对象的锁,执行代码,阻塞其他线程。

作用:

用法:

特点:

原理:

测试代码:

public class TestSynchronized {public void sync() {synchronized (this) {System.out.println("sync");}}public synchronized void syncdo() {System.out.println("syncdo");}public static synchronized void staticSyncdo() {System.out.println("staticSyncdo");}}

通过JDK 反汇编指令javap -c -v TestSynchronized

javap -c -v TestSynchronized Last modified 2019-5-27; size 719 bytesMD5 checksum e5058a43e76fe1cff6748d4eb1565658Compiled from "TestSynchronized.java"public class constxiong.interview.TestSynchronizedminor version: 0major version: 49flags: ACC_PUBLIC, ACC_SUPERConstant pool: #1 = Class#2 // constxiong/interview/TestSynchronized #2 = Utf8 constxiong/interview/TestSynchronized #3 = Class#4 // java/lang/Object #4 = Utf8 java/lang/Object #5 = Utf8 <init> #6 = Utf8 ()V #7 = Utf8 Code #8 = Methodref#3.#9// java/lang/Object."<init>":()V #9 = NameAndType#5:#6// "<init>":()V#10 = Utf8 LineNumberTable#11 = Utf8 LocalVariableTable#12 = Utf8 this#13 = Utf8 Lconstxiong/interview/TestSynchronized;#14 = Utf8 sync#15 = Fieldref #16.#18// java/lang/System.out:Ljava/io/PrintStream;#16 = Class#17// java/lang/System#17 = Utf8 java/lang/System#18 = NameAndType#19:#20// out:Ljava/io/PrintStream;#19 = Utf8 out#20 = Utf8 Ljava/io/PrintStream;#21 = String #14// sync#22 = Methodref#23.#25// java/io/PrintStream.println:(Ljava/lang/String;)V#23 = Class#24// java/io/PrintStream#24 = Utf8 java/io/PrintStream#25 = NameAndType#26:#27// println:(Ljava/lang/String;)V#26 = Utf8 println#27 = Utf8 (Ljava/lang/String;)V#28 = Utf8 syncdo#29 = String #28// syncdo#30 = Utf8 staticSyncdo#31 = String #30// staticSyncdo#32 = Utf8 SourceFile#33 = Utf8 TestSynchronized.java{public constxiong.interview.TestSynchronized();descriptor: ()Vflags: ACC_PUBLICCode:stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #8// Method java/lang/Object."<init>":()V 4: returnLineNumberTable:line 3: 0LocalVariableTable:StartLengthSlotName Signature0 5 0this Lconstxiong/interview/TestSynchronized; public void sync();descriptor: ()Vflags: ACC_PUBLICCode:stack=2, locals=2, args_size=1 0: aload_0 1: dup 2: astore_1 3: monitorenter 4: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream; 7: ldc #21 // String sync 9: invokevirtual #22 // Method java/io/PrintStream.println:(Ljava/lang/String;)V12: aload_113: monitorexit14: goto2017: aload_118: monitorexit19: athrow20: returnException table: fromtotarget type 41417 any171917 anyLineNumberTable:line 6: 0line 7: 4line 6: 12line 9: 20LocalVariableTable:StartLengthSlotName Signature021 0this Lconstxiong/interview/TestSynchronized; public synchronized void syncdo();descriptor: ()Vflags: ACC_PUBLIC, ACC_SYNCHRONIZEDCode:stack=2, locals=1, args_size=1 0: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #29 // String syncdo 5: invokevirtual #22 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: returnLineNumberTable:line 12: 0line 13: 8LocalVariableTable:StartLengthSlotName Signature0 9 0this Lconstxiong/interview/TestSynchronized; public static synchronized void staticSyncdo();descriptor: ()Vflags: ACC_PUBLIC, ACC_STATIC, ACC_SYNCHRONIZEDCode:stack=2, locals=0, args_size=0 0: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #31 // String staticSyncdo 5: invokevirtual #22 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: returnLineNumberTable:line 16: 0line 17: 8LocalVariableTable:StartLengthSlotName Signature}SourceFile: "TestSynchronized.java"