Before this tutorial please read :
There are following important differences between synchronized and ReEntrantLocks in java >
synchronized
|
ReentrantLock
|
Does not provide any fair locks in java.
|
provides fair locks, when lock is fair - first lock is obtained by longest-waiting thread in java.
Constructor to provide fairness -
ReentrantLock(boolean fair)
Creates an instance of ReentrantLock.
When fair is set true, first lock is obtained by longest-waiting thread.
If fair is set false, any waiting thread could get lock, at discretion of implementation.
|
Does not provide tryLock() method or its functionality. Thread always waits for lock in java.
|
Provide tryLock() method. If lock is held by another thread then method return false in java.
boolean tryLock()
Acquires the lock if it is not held by another thread and returns true. And sets lock hold count to 1.
If current thread already holds lock then method returns true. And increments lock hold count by 1.
If lock is held by another thread then method return false.
|
There is no method for lock interruptibility, though current thread waits until one of the following thing happens -
|
void lockInterruptibly()
If current thread already holds lock then method returns true. And increments lock hold count by 1.
If the lock is held by another thread then the current thread waits until one of the following thing happens -
As soon as current thread acquires the lock it sets lock hold count to 1.
|
Does not provide any method to return number of threads that may be waiting to acquire this lock in java.
|
provide int getQueueLength() method to return number of threads that may be waiting to acquire this lock in java.
|
holdsLock() method is used to find out whether lock is held by current thread or not. If current thread holds lock method returns true.
|
isHeldByCurrentThread() method is used to find out whether lock is held by current thread or not. If current thread holds lock method returns true in java.
|
Thread can hold lock on object monitor only once.
|
if current thread already holds lock then lock hold count is increased by 1 when lock() method is called.
method to maintain lock hold count -
void lock()
Acquires the lock if it is not held by another thread. And sets lock hold count to 1.
If current thread already holds lock then lock hold count is increased by 1.
|
Does not provide any new condition() method.
|
provides newCondition() method.
Method returns a Condition instance to be used with this Lock instance.
If lock is not fair, any waiting thread could get lock, at discretion of implementation in java.
|
So, in this thread concurrency tutorial we read what are differences between synchronized and ReEntrantLocks in java
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
Executor framework >
Executor and ExecutorService framework in java
Thread concurrency Interviews >