Java — java.util.concurrent.locks.ReentrantLock | Code Factory

Index Page : Link

Donate : Link

WordPress Blog : Link

  • It is the implementation class of Lock interface and it is the direct child class of Object.
  • Reentrant means a thread can aquire same lock multiple times without any issue.
  • Internally reentrant lock increments threads personal count whenever we call lock() method and decrement count value whenever thread calls unlock() method and lock will be released whenever count reached 0.

Constructors :

1. ReentrantLock rl = new ReentrantLock()

  • Creates an instance of ReentrantLock.

2. ReentrantLock rl = new ReentrantLock(boolean fairness)

  • Creates an instance of ReentrantLock with the given fairness policy.
  • If the fairness is true then longest waiting thread can aquire the lock if it is available that is it follows FCFS (First Come First Serve) policy.
  • If the fairness is false then which waiting thread will get the chance we can’t expect.
  • Default vale for fairness is false.

Which of the following declarations are equal :

  1. ReentrantLock rl = new ReentrantLock();
  2. ReentrantLock rl = new ReentrantLock(true);
  3. ReentrantLock rl = new ReentrantLock(false);
  4. All of the above

Important Methods of ReentrantLock :

1. public void lock()

  • Acquires the lock.

2. public boolean tryLock()

  • Acquires the lock only if it is not held by another thread at the time of invocation.

3. public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException

  • Acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.

4. public void lockInterruptibly() throws InterruptedException

  • Acquires the lock unless the current thread is interrupted.
  • Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.

5. public void unlock()

  • Attempts to release this lock.

6. public int getHoldCount()

  • Queries the number of holds on this lock by the current thread.

7. public boolean isHeldByCurrentThread()

  • Returns true iff lock is hold by current thread.

8. public final int getQueueLength()

  • Returns number of threads waiting for the lock.

9. protected Collection<Thread> getQueuedThreads()

  • Returns a collection of threads which are waiting to get the lock.

10. public final boolean hasQueuedThreads()

  • Returns true if any thread waiting to get the lock.

11. public boolean isLocked()

  • Returns true if lock is aquired by some thread.

12. public final boolean isFair()

  • Returns true if fairness policy is set with the true value.

13. protected Thread getOwner()

  • Returns the thread which aquires the lock.

Example 1 :

Example 2 :

Output :

  • If we comment lines #1 and #2 then the thread will be executed simultaneously and we will get irregular output. If we are not commenting lines #1 and #2 then the thread will be executed one by one and we will get regular output.

Example 3 :

Output :

Example 4 :

Output :