Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is ReentrantLock in Java ?Difference between ReentrantLock and synchronized keyword in Java?

user-image
Question added by Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution
Date Posted: 2015/02/12
Abdul Mateen
by Abdul Mateen , Senior Software Engineer , MercurialMinds

A reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities.

Synchronized uses intrinsic locks or monitors.

 

An intrinsic locking mechanism can have some functional limitations, such as:

 

1.) It is not possible to interrupt a thread waiting to acquire a lock (lock Interruptibly).

2.) It is not possible to attempt to acquire a lock without being willing to wait for it forever (try lock).

 

3.) Cannot implement non-block-structured locking disciplines, as intrinsic locks must be released in the same block in which they are acquired.

 

A ReentrantLock is owned by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock. This can be checked using methods isHeldByCurrentThread(), and getHoldCount().

More Questions Like This