Reprint Please specify Source:http://blog.csdn.net/xingjiarong/article/details/47916703
In the previous blog we introduced the use of the Synchronized keyword modification method, and we then introduced the Synchronized keyword. In addition to the cosmetic method, you can also modify the code block, there are 5 different uses.
First, this
synchronized(this){ //互斥代码}
This refers to the object that executes the code, and the lock that synchronized gets is the lock of this object, which is equivalent to what we discussed in our previous blog post:
publicsynchronizedvoid func(){ //互斥代码}
Second, A.class
synchronized(A.class){ //互斥代码}
Here A.class get a This class, so the synchronized keyword gets the lock is the class lock, this method with the following method function is the same:
publicstaticsynchronizedvoidfun(){ //互斥代码}
All methods that require a lock on a class cannot be executed at the same time, but it can be executed concurrently with a method that requires a lock on an object, or a method that does not require any locks.
Third, Object.getclass ()
synchronized(object.getClass){ //互斥代码}
This method is generally the same as the second, but when there is inheritance and polymorphism, the results are not the same. So it is generally recommended to use the A.class method.
Four, Object
synchronized(object){ //互斥代码}
Here the Synchronized keyword gets the lock is the object of the lock, all need to lock the object of the method can not be executed at the same time.
public Class Trans {private Object lock = new Object (); public void printnum (int num) {Synchronized (lock ) {system.. Print (Thread.CurrentThread ()); for (int i=0 ; I<25 ; i++) {System. Out . Print (I+); } System. out . println (); } }}
class MyThread implements Runnable { PrivateTrans Trans;Private intNum PublicMyThread (Trans trans,intNUM) { This. trans = trans; This. num = num; } Public voidRun () { while(true) {trans.printnum (num);Try{Thread.Sleep ( -); }Catch(Interruptedexception e) {E.printstacktrace (); } } } } Public class Test { Public Static voidMain (string[] args) {Trans T =NewTrans (); Trans T1 =NewTrans (); Thread A =NewThread (NewMyThread (T,1)); Thread B =NewThread (NewMyThread (T1,2)); A.start (); B.start (); } }
In the example above, try to use this method to achieve the mutex method printing method, but the fact is that it is not effective, because each trans object has its own object, both objects have their own locks, so two threads need to be different locks, two locks do not have any interaction between the So it does not play a mutually exclusive role.
Five, static object
The code above can be mutually exclusive, changing the declaration of object objects in the trans class to the following:
privatestaticlocknew Object();
So different classes use the same Object object, the lock required is the same lock, you can achieve mutual exclusion effect.
After two blog introduction, we discussed in detail the use of the Synchronized keyword, it seems very complex, in fact, after catching the point is very good to distinguish, as long as the synchronized obtained is which object or class of lock on the line, the other need this lock method can not be executed at the same time, Methods that do not require this lock can be executed at the same time.
Finally to say goodbye to a misunderstanding, I believe we will not make this mistake again, synchronized lock is an object or class (actually also object), not a method or code snippet.
Copyright NOTICE: This article for Bo Master original article, reproduced please indicate the source, view the original article, please visit: Http://blog.csdn.net/xingjiarong
Java Multithreading (v) Synchronized keyword Decoration code block