The Readers and Writers Examples

The readers and writers problem is one of the classical synchronization problems that is frequently encountered in the real world:  many shared resources and databases can be accessed concurrently by many clients in the read-only fashion but must be accessed exlusively by one client if something needs to be changed/updated.  Many readers can access a resource at one time but only one writer at any time may access the resource.

Simple Readers Writers Example: RWempty.java, All sources
This is a simple example that shows only the synchronization part of readers-writers problem.  No data structure is used for readers and writers to work on.  No priorites are given or specific policies are used in this example.

Example with vector as a shared data structure:  RWvector.java, All sources
In this example a vector is used as a shared data structute for readers and writers.  Readers look at the head and the tail of the vector and writers change the vector:  one writer adds to the tail of the vector and another writer removes from the head of the vector.

Example with specific notification locks: RWnotif.java, All sources
This example is from Dr. Mizuno's paper A Structured Approach for Developing Concurrent Programs in Java.  Two notification objects, one for readers and one for writers, are used in the program.  The use of specific notification locks improves the performance of the program, since only specific threads get signaled when their specific conditions become true, as apposed to signalling all waiting threads in the previous examples.

Example with a simple policy: RW.java, All sources
This example is from Doug Lea's book Concurrent Programming in Java .  The following policy is inforced:

Example with a more sophisticated policy: RWVSN.java, SpEx-JML, All sources
This example is also from Doug Lea's book Concurrent Prohramming in Java.  Here, we have a more sophisticated policy:

To implement these policies a queue of monitors is used to hold specific notification locks for Writers that get notified in FIFO order.  A single notification object is used for all Readers.