Name

ReentrantLock

Synopsis

This class implements the Lock interface and adds instrumentation methods to determine what thread currently holds the lock, to return the number of threads waiting to acquire the lock or waiting on an associated Condition, and to test whether a specified thread is waiting to acquire the lock.

The name of this class includes the term “reentrant” because the thread that holds the lock can call any of the locking methods again, and they return immediately without blocking. isHeldByCurrentThread( ) tests whether the current thread already holds the lock. getHoldCount( ) returns the number of times that the current thread has acquired this lock. unlock( ) must be called this number of times before the lock is actually relinquished.

A “fair” lock may be created by passing true to the ReentrantLock( ) constructor. If you do this, the lock will always be granted to the thread that has been waiting for it the longest.

java.util.concurrent.locks.ReentrantLock

Figure 16-105. java.util.concurrent.locks.ReentrantLock

public class ReentrantLock implements Lock, Serializable {
// Public Constructors
     public ReentrantLock( );  
     public ReentrantLock(boolean fair);  
// Public Instance Methods
     public int getHoldCount( );                                          default:0
     public final int getQueueLength( );                                  default:0
     public int getWaitQueueLength(Condition condition);  
     public final boolean hasQueuedThread(Thread thread);  
     public final boolean hasQueuedThreads( ); ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.