First, introduce
This article describes the Synchronized keyword in Java multi-threading as an object lock feature.
Second, the analysis
Synchronized can be used to modify the instance method in the following form:
1 Public class MyObject {23 synchronized Public void MethodA () {4 // Do something .... 5 }
Here, the Synchronized keyword locks the current object. This is also the reason called object lock.
Why lock the current object? Because MethodA () is an instance method, to execute MethodA (), it needs to be called in the form of an object. Method () (Obj.methoda (), obj is an object of the MyObject class, and synchronized is the object where obj is locked.
The above code can also be written like this:
1 Public class MyObject {23 Public void MethodA () {4 synchronized (this) {5 // Do something .... 6 }7 }
Three, features
One obvious feature of using the Synchronized keyword synchronization is that when multiple synchronized decorated instance methods are defined in the MyObject class, If multiple threads have objects of the same MyObject class, These methods can only be executed in a synchronous fashion . That is, after you have executed a synchronized-modified method, you can perform another method of synchronized adornment.
As follows:
1 Public classMyObject {2 3 synchronized Public voidMethodA () {4 //Do something ....5 }6 7 synchronized Public voidMethodB () {8 //Do some other thing9 }Ten}
There are two methods of synchronized modification in the MyObject class.
1 Public classThreadaextendsThread {2 3 PrivateMyObject object;4Ellipsis Construction method5 @Override6 Public voidrun () {7 Super. Run ();8 Object.methoda ();9 }Ten}
Thread A executes MethodA ()
Public class extends Thread { private MyObject object; // Ellipsis Construction Method @Override publicvoid run () { Super. Run (); Object.methodb (); }}
Thread B execution MethodB ()
Public class Run { publicstaticvoid main (string[] args) { new MyObject (); // thread A and thread B hold the same object: Object New Threada (object); New threadb (object); A.start (); B.start (); }}
Because thread A and thread B hold objects of the same MyObject class object, although these two threads need to call different methods, they must be synchronous, such as that thread B waits for thread A to execute the MethodA () method before it can execute the MethodB () method.
Iv. the conclusion
As can be seen from the above, the scope of the synchronized lock described in this article is the entire object. If there is more than one synchronized decorated synchronization method in a class, and multiple threads hold the same object of the class (the same object for that class), the execution of each method is synchronous, even though they call different methods.
If there are no shared variables between the synchronized methods, or there is no connection between the methods, it can only be performed synchronously, which can affect efficiency.
Java multi-thread synchronized keyword--Characteristics of object lock