I find out one more benefit of using ReentrantLock over synchronized
Below code shows even if exception occurs in critical section lock is released(Using ReentrantLock )
void someMethod() {
//get the lock before processing critical section.
lock.lock();
try
{
// critical section
//if exception occurs
}
finally
{
//releasing the lock so that other threads can get notifies
lock.unlock();
}
}//end of method
Now by using synchronized
void someMethod() {
//get the lock before processing critical section.
synchronized(this)
{
try
{
// critical section
//if exception occurs
}
finally
{
//I don't know how to release lock
}
}
}//end of method
by comparing both the code I see that there is one more disadvantage in using synchronized block i.e If exception occurs in critical section than it is not possible to release the lock.
Am I right ?
Correct me if I am wrong please.
Is there anyway to release lock if exception occurrs in synchronized block ?