java - Benefits of using ReentrantLock over synchronized -
this question has answer here:
i find out 1 more benefit of using reentrantlock on synchronized
below code shows if exception occurs in critical section lock released(using reentrantlock )
void somemethod() { //get lock before processing critical section. lock.lock(); try { // critical section //if exception occurs } { //releasing lock other threads can notifies lock.unlock(); } }//end of method
now using synchronized
void somemethod() { //get lock before processing critical section. synchronized(this) { try { // critical section //if exception occurs } { //i don't know how release lock } } }//end of method
by comparing both code see there 1 more disadvantage in using synchronized block i.e if exception occurs in critical section not possible release lock.
am right ?
correct me if wrong please.
is there anyway release lock if exception occurrs in synchronized block ?
if exception occurs in synchronized block lock still released. lock released in circumstances.
the advantage reentrantlock use compare , sweep performs better in low contention.
Comments
Post a Comment