Example of a simple synchronization operation using the synchronized keyword in Java

  • 2020-04-01 03:49:43
  • OfStack

Simply document the use of the synchronized keyword in Java.

Before we go into this, we need to make it clear that every object instance of a class in Java has a lock associated with it and that the synchronized keyword applies only to that lock, which means that synchronized can be considered to work only on an object instance of a Java class.

Synchronized modifier


public synchronized aMethod(){
}

This is the most common scenario, so what is the purpose of this synchronization method? For convenience, call it the aMethod method.

1. Synchronized locking is an object instance that invokes the synchronized method. For example, when the same instance P1 calls aMethod in different threads, synchronization occurs.
2. It is important to note that P2, another object in the class to which this object belongs, can call this aMethod at will because the synchronized methods of different object instances are irrelevant. That is, other threads can 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, or cMethod, no other thread can now access any of the synchronized methods in the object at the same time as one thread accesses one of them.

The above code is equivalent to the following:


public void aMethod() {  
    synchronized (this) {
    }  
}

This here refers to a reference to the instance object, such as P1. So the essence of a synchronized method is to apply synchronized to an object reference. Only the thread that gets the lock of the P1 object can call the synchronization method of P1. In the case of P2, the lock of P1 has nothing to do with him. In this case, the program may get rid of the control of the synchronization mechanism and cause data chaos. From this we derive the following synchronized block.

Synchronized modifies a block of code


public void dMethod(SomeObject so) {  
    synchronized(so) {
    }  
}

Here synchronized gets a lock, which is the lock of the object so, so that whoever gets the lock can run the piece of code he controls. This can be done when there is an explicit object as a lock, but when there is no explicit object as a lock and you just want to synchronize a piece of code, you can create a special instance variable (which must be an object) to act as a lock:

class Foo implements Runnable {
    private byte[] lock = new byte[0];
    Public void method() {
        synchronized(lock) {
        }
    }
}

Zero-length byte array objects are more economical to create than any other object.

Synchronized modifies static methods

Mentioned the synchronized keyword applies only to P1 instances of different threads, how can be effective for P1 and P2 different instances at the same time, the answer is to use synchronized static method, a class of static methods can be said to be the class of its own, does not depend on instances of the class, so we want to the class the static method USES synchronized keyword to modify can achieve synchronization between different instances.


Related articles: