org.metasyntactic.thread.concurrent
Class WriterPreferenceReadWriteLock

java.lang.Object
  |
  +--org.metasyntactic.thread.concurrent.WriterPreferenceReadWriteLock
All Implemented Interfaces:
ReadWriteLock
Direct Known Subclasses:
ReaderPreferenceReadWriteLock

public class WriterPreferenceReadWriteLock
extends java.lang.Object
implements ReadWriteLock

A ReadWriteLock that prefers waiting writers over waiting readers when there is contention. This class is adapted from the versions described in CPJ, improving on the ones there a bit by segregating reader and writer wait queues, which is typically more efficient.

The locks are NOT reentrant. In particular, even though it may appear to usually work OK, a thread holding a read lock should not attempt to re-acquire it. Doing so risks lockouts when there are also waiting writers.


Field Summary
protected  java.lang.Thread activeWriter
          A reference to the actual writer currently writing
 
Constructor Summary
WriterPreferenceReadWriteLock()
           
 
Method Summary
 Monitor readLock()
          Get's this Lock's read monitor.
 Monitor writeLock()
          Get's this Lock's writer monitor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

activeWriter

protected java.lang.Thread activeWriter
A reference to the actual writer currently writing

Constructor Detail

WriterPreferenceReadWriteLock

public WriterPreferenceReadWriteLock()
Method Detail

writeLock

public Monitor writeLock()
Get's this Lock's writer monitor. The caller must then call .acquire() on thie monitor before they begin writing

Specified by:
writeLock in interface ReadWriteLock
Returns:
The write lock monitor

readLock

public Monitor readLock()
Get's this Lock's read monitor. The caller must then call .acquire() on thie monitor before they begin Reading

Specified by:
readLock in interface ReadWriteLock
Returns:
The read monitor