How to implement Synchronized
synchronized
Types can be divided into two categories:
- Synchronized method
- Synchronized block
The implementation of the two is different, the JVM specification writes that the compiled synchronized method will have a acc_synchronized flag, that is, when the JVM methods invoke the command (Invoca tion instruction) found this method from the Run-time constant pool, already knew that it was a synchronized method, so the lock operation was called by methods and Returns the command to control.
The lock of the synchronized block is monitorenter
controlled by the monitorexit
two commands.
You can "disassemble" the class file by using the JAVAP command.
publicclass StaticMethodTest { publicstaticsynchronizedvoidstaticMethod() { } publicstaticvoidstaticMethod1() { synchronized (StaticMethodTest.class) { // ... } } publicvoidmemberMethod() { } }
Executes after compiling into a class file
-verbose StaticMethodTest
The output results of two static methods are
Public StaticSynchronizedvoid Staticmethod(); Descriptor: () V flags:acc_public, acc_static, acc_synchronized code:stack=0, locals=0, args_size=0 0:returnLinenumbertable:line2:0 Public Static void staticMethod1(); Descriptor: () V flags:acc_public, acc_static code:stack=2, locals=2, args_size=0 0: LDC#2//class Staticmethodtest 2: DUP3: Astore_04: Monitorenter5: Aload_06: Monitorexit7:Goto the Ten: Astore_1 One: Aload_0 A: Monitorexit -: Aload_1 -: Athrow the:returnException table: fromTo target type5 7 TenAnyTen - TenAny
In fact, whether static or member methods are synchronized
implemented the same way, then what is the relationship between the class and the object?
Classes and objects
The first thing to know is how the class comes into being.
After the JVM gets the compiler-compiled class file, it will first load the file into memory, and the class file will certainly have its own format, so the content of the file needs to be parsed by ClassLoader, and the parsed content will use an Class
instance of the class-class object To indicate that this object can be obtained from Java. ClassName.class
That is, the Class object is an instance of a Class
type (instance), and the object is a ClassName instance. Class and ClassName are types, classname are defined by class
keywords, and class is a built-in type.
The Synchronized method of the member methods is therefore equivalent to the synchronized (this) block, i.e. the following two methods are equivalent.
publicsynchronizedvoidfun1() { // do something here}
publicsynchronizedvoidfun2() { synchronized (this) { // do something here }}
The Member method belongs this
to, and the static method belongs to Class Object, then the synchronized method of static methods is equivalent to the following form of synchronized block.
publicstaticsynchronizedvoidfun2() { synchronized (ClassName.class) { // do something here }}
Validation code
Three static methods are defined, each with a different locking mechanism, and each method is a dead loop. Then define three threads to execute each of the three methods.
Importjava.util.concurrent.*; Public class synchronizedtest { Private StaticObject lock =NewObject (); Public synchronized Static void Fun() { while(true) {System.out.println ("In the Fun");Try{TimeUnit.MILLISECONDS.sleep ( -); }Catch(Interruptedexception e) {E.printstacktrace (); } } } Public Static void fun1() {synchronized(Synchronizedtest.class) { while(true) {System.out.println ("in Fun1");Try{TimeUnit.MILLISECONDS.sleep ( -); }Catch(Interruptedexception e) {E.printstacktrace (); } } } } Public Static void fun2() {synchronized(lock) { while(true) {System.out.println ("in Fun2");Try{TimeUnit.MILLISECONDS.sleep ( -); }Catch(Interruptedexception e) {E.printstacktrace (); } } } } Public Static void Main(string[] args) {NewThread () {@Override Public void Run() {fun (); }}.start ();NewThread () {@Override Public void Run() {fun1 (); }}.start ();NewThread () {@Override Public void Run() {fun2 (); }}.start (); }}
The result of the operation is in the fun and in fun2 alternating, that is, there is no in fun1.
As long as the thread that runs the fun does not hand over the lock, FUN1 cannot be called because they are all locks that share the Class object.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to use synchronized in static methods