|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectedu.rice.cs.plt.concurrent.ReaderWriterLock
public class ReaderWriterLock
This class implements synchronization primitives to solve the classic readers/writers problem without allowing deadlock or starvation.
Problem: Suppose multiple threads want to read and write to a resource. Multiple readers can be active at a time, but only a single writer can be active at a time, and no readers can be active while the writer is active.
We must be careful to avoid starvation in our solution, so that a steady flow of reader threads cannot block waiting writer threads indefinitely. This can be achieved by imposing an ordering on the incoming readers and writers.
To use this class, instantiate a ReaderWriterLock in the class holding the shared resource. Any methods which read from the resource must call startRead before reading and endRead after reading, and must not be synchronized themselves. Similarly, any methods which write to the resource must not be synchronized and must call startWrite before writing and endWrite after writing.
This class enforces that any readers and writers that are forced to wait are allowed access to the shared resource in the order in which they arrive. Groups of readers are allowed to proceed simultaneously, where each group contains all waiting readers that arrived before the next waiting writer.
This class is loosely adapted from the "Starvation-Free Readers and Writers Monitor" available from Stephen J. Hartley (Drexel University) at: http://www.mcs.drexel.edu/~shartley/ConcProgJava/monitors.html We have imposed an ordering on the pending waiting readers and writers using an ordered queue.
| Nested Class Summary | |
|---|---|
class |
ReaderWriterLock.Reader
Object representing a reader thread which is waiting for read access on the queue of waiting threads. |
class |
ReaderWriterLock.ReaderWriterThread
Represents a thread waiting to either read or write. |
class |
ReaderWriterLock.Writer
Object representing a writer thread which is waiting for write access on the queue of waiting threads. |
| Constructor Summary | |
|---|---|
ReaderWriterLock()
Creates a new ReaderWriterLock. |
|
| Method Summary | |
|---|---|
void |
endRead()
Must be called by each reader thread after it is finished reading. |
void |
endWrite()
Must be called by each writer thread after it is finished writing. |
void |
startRead()
Must be called by each reader thread before starting to read. |
void |
startWrite()
Must be called by each writer thread before starting to write. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ReaderWriterLock()
| Method Detail |
|---|
public void startRead()
IllegalStateException - if the thread is already a reader or writerpublic void endRead()
IllegalStateException - if the thread is already a reader or writerpublic void startWrite()
IllegalStateException - if the thread is already a reader or writerpublic void endWrite()
IllegalStateException - if the thread is already a reader or writer
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||