I'm having some trouble wrapping my head around the concept of synchronized blocks in Java. I feel I have understood synchronized methods well enough. So I thought of an analogy to help me understand synchronized blocks in terms of synchronized methods. Please let me know if this equivalence that I have proposed is correct. Also, I have only mentioned this for a non-static synchronized block for now. However, points on static synchronzied blocks are also welcome.
public void method()
{
//code snipppet A
synchronized(objRef)
{
//code snipppet B
}
//code snipppet C
}
Is equivalent to
public void method() {
//code snippet A
objRef.sync_method();
//code snippet C
}
In the class of objRef:
public synchronized void sync_method() {
//code snippet B
}
This analogy is based on the logic that synchronized blocks behave just as synchronized methods do. Meaning that once a thread acquires a lock on the monitor, it does not allow other threads to interfere in its execution. The thread relinquishes control only once it has finished executing the entire synchronized method.
You should notice that there are two types of synchronized block in Java (object synchronize and class synchronize). There are two synchronized methods (static and non-static methods) and their equivalent synchronized blocks (Class and Object Synchronized blocks respectively). assume we have:
1. Object Synchronization:
Is equivalent to
2. Class Synchronization:
Is equivalent to
There is a possibility that you're not getting everything right. For example,
is not equivalent to
because in the former case the lock covers a larger part of the code. Equivalence exists between
and
Further,
The above may or may not be true, depending on how we interpret the ambiguous wording. A thread that has acquired a lock may easily allow other threads to interfere with its execution: all it takes is another piece of code not covered by exactly the same lock. For example,
The call to
sync_method()
can print both 0 and 42.