Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 830
نمايش فراداده

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.

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 public int

getReadLockCount ( ); default:0 public int

getWaitQueueLength (Condition

condition ); public int

getWriteHoldCount ( ); default:0 public final boolean

hasQueuedThread (Thread

thread ); public final boolean

hasQueuedThreads ( ); public boolean

hasWaiters (Condition

condition ); public final boolean

isFair ( ); default:false public boolean

isWriteLocked ( ); default:false public boolean

isWriteLockedByCurrentThread ( ); default:false public ReentrantReadWriteLock.ReadLock

readLock ( ); public ReentrantReadWriteLock.WriteLock

writeLock ( ); // Public Methods Overriding Object public String

toString ( ); // Protected Instance Methods protected Thread

getOwner ( ); protected java.util.Collection<Thread>

getQueuedReaderThreads ( ); protected java.util.Collection<Thread>

getQueuedThreads ( ); protected java.util.Collection<Thread>

getQueuedWriterThreads ( ); protected java.util.Collection<Thread>

getWaitingThreads (Condition

condition ); }

Passed To

ReentrantReadWriteLock.ReadLock.ReadLock( ),

ReentrantReadWriteLock.WriteLock.WriteLock( )