Name

ReentrantReadWriteLock

Synopsis

This class implements the ReadWriteLock interface. The locks returned by the readLock( ) and writeLock( ) methods are instances of the inner classes ReadLock and WriteLock. ReentrantReadWriteLock defines a “fair mode” and includes instrumentation methods like ReentrantLock does.

Any number of threads can acquire the read lock as long as no thread holds or is attempting to acquire the write lock. When a thread attempts to acquire the write lock, no new read locks are granted. When all existing readers have relinquished the lock, the writer acquires the lock, and no reads are allowed until the writer has relinquished it. A thread that holds the write lock may downgrade to a read lock by acquiring the read lock and then relinquishing the write lock.

Because the read lock is not exclusive, it cannot have a Condition associated with it. The ReadLock.newCondition( ) method throws UnsupportedOperationException.

java.util.concurrent.locks.ReentrantReadWriteLock

Figure 16-106. java.util.concurrent.locks.ReentrantReadWriteLock

public class ReentrantReadWriteLock implements ReadWriteLock, Serializable {
// Public Constructors
     public ReentrantReadWriteLock( );  
     public ReentrantReadWriteLock(boolean fair);  
// Nested Types
     public static class ReadLock implements Lock, Serializable; 
     public static class WriteLock implements Lock, Serializable; 
// Public Instance Methods
     public final int getQueueLength( );                                  default:0 ...

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.