org.sandev.basics.util
Class ReaderWriterLock

java.lang.Object
  extended byorg.sandev.basics.util.ReaderWriterLock

public class ReaderWriterLock
extends java.lang.Object

A multiple reader single writer lock utility.


Field Summary
protected  int activeReaders
          The number of current readers.
protected  int activeWriters
          The number of current writers.
protected  int waitingReaders
          The number of waiting readers.
protected  java.util.List writerLocks
          Our list of waiting writers.
 
Constructor Summary
ReaderWriterLock()
           
 
Method Summary
protected  void notifyReaders()
          Notify waiting reader threads.
protected  void notifyWriters()
          Release the next queued writer.
 void readCompleted()
          Relinquish a read lock.
 void requestRead()
          Request a read lock and block until it happens.
 void requestWrite()
          Request the write lock and block until it happens.
 void writeCompleted()
          Relinquish the write lock.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

activeReaders

protected int activeReaders
The number of current readers.


waitingReaders

protected int waitingReaders
The number of waiting readers. These are waiting because there is a write in progress.


activeWriters

protected int activeWriters
The number of current writers.


writerLocks

protected final java.util.List writerLocks
Our list of waiting writers. These go one at a time in the order they were registered. The list length is the number of waiting writers.

Constructor Detail

ReaderWriterLock

public ReaderWriterLock()
Method Detail

requestRead

public void requestRead()
Request a read lock and block until it happens. Caller must finally call readCompleted so the counts don't get out of whack.


readCompleted

public void readCompleted()
Relinquish a read lock. Caller must have previously acquired a lock through requestRead or the counts will get out of whack.


requestWrite

public void requestWrite()
Request the write lock and block until it happens. Caller must finally call writeCompleted so the counts don't get out of whack.


writeCompleted

public void writeCompleted()
Relinquish the write lock. Caller must have previously acquired a lock through requestWrite or the counts will get out of whack.


notifyReaders

protected void notifyReaders()
Notify waiting reader threads. Must be called from within a synchronized method.


notifyWriters

protected void notifyWriters()
Release the next queued writer. Must be called from within a synchronized method.