Thomas W. Christopher Consulting in High Performance Computing.
Training in Concurrency, Networking, and Parallelism, especially in Python and Java.

Animation of Queued-Readers-Writers Monitor


This page animates the Queued-Readers-Writers policy for coordinating multiple readers and writers of a shared resource.



Thomas W. Christopher
Tools of Computing
tc AT tools HYPHEN of HYPHEN computing DOT com


This requires Java JDK1.1 or greater. It uses the 1.1 event model that is not implemented in earlier versions of Java.


TestQueuedReadersWriters The Queued-Readers-Writers Monitor

This monitor uses the classes FutureQueue and Future from the Tools of Computing thread package. As each reader or writer arrives, it takes the next Future from a FutureQueue and waits for a value to be assigned to that future, giving it permission to read or to attempt to write.

State of the monitor

The state of this monitor is contained in three variables:

nr: the number of threads currently reading, >=0
fq: the FutureQueue which queues up the threads awaiting permission to read or write.


If a thread tries to start reading, it takes the token from fq. This token allows it to start reading. It places the token back in the queue immediately, allowing the next thread, if present and if a reader, to start reading too. The readers also increment the count of the number of readers present. If a writer gets the token after a series of readers, it will wait until all the readers are done before proceeding.

   public void startReading() 
		throws InterruptedException{
	Future f=fq.get();
	synchronized (this) {nr++;}


To start writing, a thread not only must get the token, but must wait for the number of readers to go to zero. A writer does not immediately place the token back in the queue because no subsequent reader or writer may proceed until it is done. It waits until it stops writing to place the token into fq.

  public void startWriting() 
		throws InterruptedException{
	Future f=fq.get();
	synchronized (this) {while(nr>0) wait();}


When a reader present finishes reading, it decrements the count of readers. There may be a writer waiting for the count to become zero, so the last reader to finish calls notify() to allow the writer (if any) to proceed.

  public synchronized void stopReading(){
	if (nr==0) notify();


When a writer finishes writing, it puts the token (null) into the FutureQueue, allowing the next reader or writer to proceed.

 public synchronized void stopWriting(){