Synchronization – The Question Answer way!
Q. What is the difference between Synchronization Method & Synchronization Block.
They can be differentiated on two basis:-
1) The nature of lock:-
Synchronized method of an object is synchronized on the lock of the object or class (static synchronized method) of the method, while
Synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.
2) Granularity of lock:-
Synchronization Block mechanism allows fine-grained synchronization, while
Synchronized method are to be used when a method can manipulate the state of the object in ways that can corrupt the state if executed concurrently.
Q. What are Locks ?
A. Lock is used to synchronize access to shared resources
A thread gain access to a shared resource by first acquiring the lock associated with the resource
A lock thus implements mutual exclusion
All objects have a lock, only one thread at a time can access the shared resource guarded by the lock.
Q. Number of ways access of the resource can be synchronized ?
There are two ways in which access of the resource can be syncronized
a. Synchronized method
b. Synchronized block
a. Synchronized method:-
A thread wish to execute synchronized method must first obtain the OBJECTS LOCK before it can enter the object to execute the method.
In other words, Synchronized method of an object is synchronized on the lock of the object
Q. How is lock of the object achieved ?
A. This is simply achieved Lock is by calling the method, if the object’s lock is acquired by some other thread, the calling thread waits.
Q. When to Use synchronized method ?
Synchronized method are to be used when a method can manipulate the state of the object in ways that can corrupt the state if executed concurrently.
Example:- Stack Push & Pop methods. When one thread is pushing other thread can not pop.
When one thread is executing the synchronized method of an object, all other threads that wish to execute this or any other sync. method have to wait.
This does not applies to thread that already has the lock, such a method can call other sync. method without being blocked.
Important Note:-
Q. How Static method are synchronized ?
Static method synchronize on the class lock.
Also, synchronization of static method in a class is independent from the synchronization of instance method of the class.
Q. A synchronized method, when overridden in sub class, what happens to its synchronized quality ?
A sub class decides whether the new definition of an inherited synchronized method will remain synchronized in the subclass.
b. Synchronized block
Synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.
synchronized (<object reference expression>) {<Code block>}
“object reference expression” must evaluate to a non null reference value, else a null pointer exception is thrown
The code block is usually related to the object on which synchronization is done.
example:- synchronized (this) {}
Once the thread had entered the code block, no other thread will be able to execute the code block, or any other code requiring the same object lock.
This mechanism allows fine-grained synchronization of code on arbitrary object.
Q. Synchronization Block, are all part of the sytex mandatory?
“synchronized (<object reference expression>) {<Code block>}”
Object specification & {} are mandatory even if we have a single line of code.
Q. An inner object might need to synchronize on its associated outer object, how is it achievable ?
class Outer{
private double myPi;
protected class inner{
public void setPi(){
synchronized void setPi(Outer.this){
myPi = Math.PI;
}
}
}
}
Q. Can Synchronized block be specified on class lock ?
Yes, by:-
synchronized (<class name>.class) {<code block>}
A static synchronized method of class A is equivalent to
static void classAction(){
synchronized (A.class) {
…
}
}
Q. In how many ways a thread can hold a lock on an object ?
1) By executing a member synchronized method, object lock.
2) By executing a static synchronized method, class lock.
3) By executing a synchronized block, lock on an object or class.