org.metasyntactic.thread
Interface ReaderWriterMonitor

All Known Implementing Classes:
AlternatingReadersWritersMonitor, ReadersPreferredMonitor, SingleReaderWriterMonitor, TakeANumberMonitor, WritersPreferredMonitor

public interface ReaderWriterMonitor

Threads that share data structures can run into problems if one of the threads is writing into the structure while another thread is reading it, or if two threads try to write into it at the same time. This is because typically several values must be stored to move the data structure from one consistent state to another. When only some of the stores have been made when it is read, the reader will get confused. If more than one thread is writing into it, some of the fields may end up changed by one of the threads and other fields changed by the other, and thereafter anyone reading it will be confused.

The easiest solution is to lock the data structure whenever accessing it. Any other thread trying to access the structure will have to wait until it is unlocked before being able to lock the structure itself.

The problem with locking the data structure is this: although it is essential to prevent any thread from writing into the data structure while another thread is writing or reading, there is no reason to prevent several threads from simultaneously reading. If many threads are permitted to read at the same time, we would expect better performance.

There are (so-far) implementations of this class that contain different policies for synchronizing threads trying to read or write a shared data structure.

Readers and writers must explicitly lock and unlock the shared resource. The locking is done via method calls to a monitor object that implements the desired policy:

Reader

 monitor.startReading();
 try {
	 //...read...
 } finally {
	 monitor.stopReading();
 }
 

Writer

 monitor.startWriting();
 try {
	 //...write...
 } finally {
	 monitor.stopWriting();
 }
 

The reason for the try...finally statements is that the thread could throw an exception within the code to read or write. It is important to unlock shared resources upon abnormal exit.

See Also:
SingleReaderWriterMonitor, WritersPreferredMonitor, ReadersPreferredMonitor, AlternatingReadersWritersMonitor, TakeANumberMonitor

Method Summary
 void startReading()
          Called to begin reading the shared data structure.
 void startWriting()
          Called to begin writing the shared data structure.
 void stopReading()
          Called when the thread is finished reading the shared data structure.
 void stopWriting()
          Called when the thread is finished writing the shared data structure.
 

Method Detail

startReading

public void startReading()
                  throws java.lang.InterruptedException
Called to begin reading the shared data structure. exception InterruptedException If interrupted while waiting for access.

Throws:
java.lang.InterruptedException - When the thread is interrupted before starting

stopReading

public void stopReading()
Called when the thread is finished reading the shared data structure.


startWriting

public void startWriting()
                  throws java.lang.InterruptedException
Called to begin writing the shared data structure. exception InterruptedException If interrupted while waiting for access.

Throws:
java.lang.InterruptedException - When the thread is interrupted before starting

stopWriting

public void stopWriting()
Called when the thread is finished writing the shared data structure.