reentrantreadwritelock.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 1,180 行 · 第 1/4 页
JAVA
1,180 行
* monitor lock.
*
* <ul>
*
* <li>If this write lock is not held when any {@link
* Condition} method is called then an {@link
* IllegalMonitorStateException} is thrown. (Read locks are
* held independently of write locks, so are not checked or
* affected. However it is essentially always an error to
* invoke a condition waiting method when the current thread
* has also acquired read locks, since other threads that
* could unblock it will not be able to acquire the write
* lock.)
*
* <li>When the condition {@link Condition#await() waiting}
* methods are called the write lock is released and, before
* they return, the write lock is reacquired and the lock hold
* count restored to what it was when the method was called.
*
* <li>If a thread is {@link Thread#interrupt interrupted} while
* waiting then the wait will terminate, an {@link
* InterruptedException} will be thrown, and the thread's
* interrupted status will be cleared.
*
* <li> Waiting threads are signalled in FIFO order.
*
* <li>The ordering of lock reacquisition for threads returning
* from waiting methods is the same as for threads initially
* acquiring the lock, which is in the default case not specified,
* but for <em>fair</em> locks favors those threads that have been
* waiting the longest.
*
* </ul>
* @return the Condition object
*/
public Condition newCondition() {
return new CondVar(this);
}
/**
* Returns a string identifying this lock, as well as its lock
* state. The state, in brackets includes either the String
* "Unlocked" or the String "Locked by"
* followed by the {@link Thread#getName} of the owning thread.
* @return a string identifying this lock, as well as its lock state.
*/
public String toString() {
Thread owner = getOwner();
return super.toString() + ((owner == null) ?
"[Unlocked]" :
"[Locked by thread " + owner.getName() + "]");
}
public boolean isHeldByCurrentThread() {
return isWriteLockedByCurrentThread();
}
}
// Instrumentation and status
/**
* Returns true if this lock has fairness set true.
* @return true if this lock has fairness set true.
*/
public final boolean isFair() {
return false;
}
/**
* Returns the thread that currently owns the write lock, or
* <tt>null</tt> if not owned. Note that the owner may be
* momentarily <tt>null</tt> even if there are threads trying to
* acquire the lock but have not yet done so. This method is
* designed to facilitate construction of subclasses that provide
* more extensive lock monitoring facilities.
* @return the owner, or <tt>null</tt> if not owned.
*/
protected synchronized Thread getOwner() {
return activeWriter_;
}
/**
* Queries the number of read locks held for this lock. This
* method is designed for use in monitoring system state, not for
* synchronization control.
* @return the number of read locks held.
*/
public synchronized int getReadLockCount() {
return activeReaders_;
}
/**
* Queries if the write lock is held by any thread. This method is
* designed for use in monitoring system state, not for
* synchronization control.
* @return <tt>true</tt> if any thread holds the write lock and
* <tt>false</tt> otherwise.
*/
public synchronized boolean isWriteLocked() {
return activeWriter_ != null;
}
/**
* Queries if the write lock is held by the current thread.
* @return <tt>true</tt> if the current thread holds the write lock and
* <tt>false</tt> otherwise.
*/
public synchronized boolean isWriteLockedByCurrentThread() {
return activeWriter_ == Thread.currentThread();
}
/**
* Queries the number of reentrant write holds on this lock by the
* current thread. A writer thread has a hold on a lock for
* each lock action that is not matched by an unlock action.
*
* @return the number of holds on the write lock by the current thread,
* or zero if the write lock is not held by the current thread.
*/
public synchronized int getWriteHoldCount() {
return writeHolds_;
}
// /**
// * Returns a collection containing threads that may be waiting to
// * acquire the write lock. Because the actual set of threads may
// * change dynamically while constructing this result, the returned
// * collection is only a best-effort estimate. The elements of the
// * returned collection are in no particular order. This method is
// * designed to facilitate construction of subclasses that provide
// * more extensive lock monitoring facilities.
// * @return the collection of threads
// */
// protected Collection getQueuedWriterThreads() {
// return sync.getExclusiveQueuedThreads();
// }
//
// /**
// * Returns a collection containing threads that may be waiting to
// * acquire the read lock. Because the actual set of threads may
// * change dynamically while constructing this result, the returned
// * collection is only a best-effort estimate. The elements of the
// * returned collection are in no particular order. This method is
// * designed to facilitate construction of subclasses that provide
// * more extensive lock monitoring facilities.
// * @return the collection of threads
// */
// protected Collection getQueuedReaderThreads() {
// return sync.getSharedQueuedThreads();
// }
//
// /**
// * Queries whether any threads are waiting to acquire the read or
// * write lock. Note that because cancellations may occur at any
// * time, a <tt>true</tt> return does not guarantee that any other
// * thread will ever acquire a lock. This method is designed
// * primarily for use in monitoring of the system state.
// *
// * @return true if there may be other threads waiting to acquire
// * the lock.
// */
// public final boolean hasQueuedThreads() {
// return sync.hasQueuedThreads();
// }
//
// /**
// * Queries whether the given thread is waiting to acquire either
// * the read or write lock. Note that because cancellations may
// * occur at any time, a <tt>true</tt> return does not guarantee
// * that this thread will ever acquire a lock. This method is
// * designed primarily for use in monitoring of the system state.
// *
// * @param thread the thread
// * @return true if the given thread is queued waiting for this lock.
// * @throws NullPointerException if thread is null
// */
// public final boolean hasQueuedThread(Thread thread) {
// return sync.isQueued(thread);
// }
/**
* Returns an estimate of the number of threads waiting to acquire
* either the read or write lock. The value is only an estimate
* because the number of threads may change dynamically while this
* method traverses internal data structures. This method is
* designed for use in monitoring of the system state, not for
* synchronization control.
* @return the estimated number of threads waiting for this lock
*/
public final synchronized int getQueueLength() {
return waitingWriters_ + waitingReaders_;
}
// /**
// * Returns a collection containing threads that may be waiting to
// * acquire either the read or write lock. Because the actual set
// * of threads may change dynamically while constructing this
// * result, the returned collection is only a best-effort estimate.
// * The elements of the returned collection are in no particular
// * order. This method is designed to facilitate construction of
// * subclasses that provide more extensive monitoring facilities.
// * @return the collection of threads
// */
// protected Collection getQueuedThreads() {
// return sync.getQueuedThreads();
// }
//
// /**
// * Queries whether any threads are waiting on the given condition
// * associated with the write lock. Note that because timeouts and
// * interrupts may occur at any time, a <tt>true</tt> return does
// * not guarantee that a future <tt>signal</tt> will awaken any
// * threads. This method is designed primarily for use in
// * monitoring of the system state.
// * @param condition the condition
// * @return <tt>true</tt> if there are any waiting threads.
// * @throws IllegalMonitorStateException if this lock
// * is not held
// * @throws IllegalArgumentException if the given condition is
// * not associated with this lock
// * @throws NullPointerException if condition null
// */
// public boolean hasWaiters(Condition condition) {
// if (condition == null)
// throw new NullPointerException();
// if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
// throw new IllegalArgumentException("not owner");
// return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
// }
// /**
// * Returns an estimate of the number of threads waiting on the
// * given condition associated with the write lock. Note that because
// * timeouts and interrupts may occur at any time, the estimate
// * serves only as an upper bound on the actual number of waiters.
// * This method is designed for use in monitoring of the system
// * state, not for synchronization control.
// * @param condition the condition
// * @return the estimated number of waiting threads.
// * @throws IllegalMonitorStateException if this lock
// * is not held
// * @throws IllegalArgumentException if the given condition is
// * not associated with this lock
// * @throws NullPointerException if condition null
// */
// public int getWaitQueueLength(Condition condition) {
// if (condition == null)
// throw new NullPointerException();
// if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
// throw new IllegalArgumentException("not owner");
// return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
// }
//
// /**
// * Returns a collection containing those threads that may be
// * waiting on the given condition associated with the write lock.
// * Because the actual set of threads may change dynamically while
// * constructing this result, the returned collection is only a
// * best-effort estimate. The elements of the returned collection
// * are in no particular order. This method is designed to
// * facilitate construction of subclasses that provide more
// * extensive condition monitoring facilities.
// * @param condition the condition
// * @return the collection of threads
// * @throws IllegalMonitorStateException if this lock
// * is not held
// * @throws IllegalArgumentException if the given condition is
// * not associated with this lock
// * @throws NullPointerException if condition null
// */
// protected Collection getWaitingThreads(Condition condition) {
// if (condition == null)
// throw new NullPointerException();
// if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
// throw new IllegalArgumentException("not owner");
// return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
// }
/**
* Returns a string identifying this lock, as well as its lock state.
* The state, in brackets, includes the String "Write locks ="
* followed by the number of reentrantly held write locks, and the
* String "Read locks =" followed by the number of held
* read locks.
* @return a string identifying this lock, as well as its lock state.
*/
public synchronized String toString() {
return super.toString() +
"[Write locks = " + getWriteHoldCount() +
", Read locks = " + getReadLockCount() + "]";
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?