Simply record how to use the synchronized keyword in java.
Before the introduction, it is necessary to clarify that each object instance of a class in Java has and has only one lock (lock) associated with it. The synchronized keyword only acts on this lock, that is, it can be considered that synchronized only affects object instances of the Java class. effect.
synchronized modified function
Copy the code code as follows:
public synchronized aMethod(){
}
This is the most commonly used scenario, so what is the purpose of this synchronization method? For convenience, it is called aMethod method.
1. What synchronized locks is the object instance that calls this synchronization method. For example, synchronization will occur when the same instance P1 calls aMethod in different threads;
2. It should be noted that another object P2 of the class to which this object belongs can call this aMethod arbitrarily, because the synchronized methods of different object instances do not interfere with each other. In other words, other threads can still access the aMethod method in another object instance of the same class at the same time;
3. If an object has multiple synchronized methods, such as aMethod, bMethod, and cMethod, now as long as one thread accesses one of the synchronized methods, other threads cannot access any synchronized method in the object at the same time.
The above code is actually equivalent to the following:
Copy the code code as follows:
public void aMethod() {
synchronized (this) {
}
}
This here refers to the reference of the instance object, such as P1. It can be seen that the essence of the synchronization method is to apply synchronized to the object reference. Only the thread that has obtained the P1 object lock can call the synchronization method of P1. As far as P2 is concerned, the P1 lock has nothing to do with it. The program may also get rid of the control of the synchronization mechanism in this situation, causing data confusion. From this we derive the synchronization block below.
synchronized modified code block
Copy the code code as follows:
public void dMethod(SomeObject so) {
synchronized(so) {
}
}
The lock obtained by synchronized here is the lock of the so object, so whoever gets the lock can run the code he controls. When there is a clear object as the lock, you can write the program like this, but when there is no clear object as the lock and you just want to synchronize a piece of code, you can create a special instance variable (it must be an object) to act as the lock:
Copy the code code as follows:
class Foo implements Runnable {
private byte[] lock = new byte[0];
Public void method() {
synchronized(lock) {
}
}
}
A zero-length byte array object will be more cost-effective to create than any other object.
synchronized modified static method
As mentioned earlier, the synchronized keyword is only valid for P1 instances in different threads. So how can it be valid for different instances of P1 and P2 at the same time? The answer is to use synchronized to modify static methods. The static methods of a class can be said to be owned by this class. It does not rely on instances of the class, so we only need to use the synchronized keyword to modify the static methods of the class to achieve synchronization between different instances.