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. Figure 16-105. java.util.concurrent.locks.ReentrantLockpublic 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 ( ); public boolean hasWaiters (Condition condition ); public final boolean isFair ( ); default:false public boolean isHeldByCurrentThread ( ); default:false public boolean isLocked ( ); default:false // Methods Implementing Lock public void lock ( ); public void lockInterruptibly ( ) throws InterruptedException; public Condition newCondition ( ); public boolean tryLock ( ); public boolean tryLock (long timeout , java.util.concurrent.TimeUnit unit ) throws InterruptedException; public void unlock ( ); // Public Methods Overriding Object public String toString ( ); // Protected Instance Methods protected Thread getOwner ( ); protected java.util.Collection<Thread> getQueuedThreads ( ); protected java.util.Collection<Thread> getWaitingThreads (Condition condition ); } |