What is Synchronization ?
Synchronization is a way to control the access to a shared resource in multithreaded environment in such a way that only one thread can access a shared resource at a time. Synchronization is achieved through the concept of monitor. A monitor is an exclusive lock that is obtained on an object by a thread when a synchronized method is called on the object ( or a synchronized block is executed ).
Intrinsic lock / Monitor lock.
Intrinsic lock or monitor lock plays an important role in different aspects of synchronization :
Reentrant synchronization
Allowing a thread to acquire a lock more than once is called Reentrant synchronization. This can happen in a scenario where one synchronized code , directly or indirectly invokes a method that also contains synchronized code , and both the code use the same lock . Without reentrant synchronization , synchronized code would have to take many additional precautions to avoid having a thread cause itself to block .
Limitations of Synchronization
Synchronization can only make sure that a shared object can be accessed by one thread at a time . It can not determine order of the usage . Sometimes there is a need to use a common resource in a specific order by multiple threads .
Synchronization is a way to control the access to a shared resource in multithreaded environment in such a way that only one thread can access a shared resource at a time. Synchronization is achieved through the concept of monitor. A monitor is an exclusive lock that is obtained on an object by a thread when a synchronized method is called on the object ( or a synchronized block is executed ).
Intrinsic lock / Monitor lock.
Intrinsic lock or monitor lock plays an important role in different aspects of synchronization :
- It enforces exclusive access to object's state .
- It establishes Happens-before relationship that is essential for visibility .
Syntax.
synchronized keyword is used to denote that a particular block of code is to be executed in a monitor. Synchronization can be done in two ways :
1. Synchronized methods .
synchronized keyword can be used with both instance methods and static methods .
// Synchronizing instance methods . Lock will be taken on the current object i.e object used to call the method doStuff(). public synchronized void doStuff(){ // Statements to be executed in monitor. } // Synchronizing static methods . Lock will be taken on class instance ( java.lang.Class ) public static synchronized void doSomeExtra(){ // Statements to be executed in monitor. }
2. Synchronized statements.
It is useful for improving concurrency with fine grained synchronization.
// Synchronizing blocks . Lock will be taken on the current object i.e object used to call the method doStuff(). public void doStuff(){ // Statements that are not executed in monitor. synchronized(this){ // Statements to be executed in monitor. } }
Reentrant synchronization
Allowing a thread to acquire a lock more than once is called Reentrant synchronization. This can happen in a scenario where one synchronized code , directly or indirectly invokes a method that also contains synchronized code , and both the code use the same lock . Without reentrant synchronization , synchronized code would have to take many additional precautions to avoid having a thread cause itself to block .
Limitations of Synchronization
Synchronization can only make sure that a shared object can be accessed by one thread at a time . It can not determine order of the usage . Sometimes there is a need to use a common resource in a specific order by multiple threads .