Readers and Writers Lab

Readers and Writers is a classic synchronization problem. Variations of the problem arise in file management systems, where several programs are trying to access (read and/or write) the same collection of files at the same time. Here are the basic definitions:

·       A bounded buffer is a message queue of limited capacity.

·       A reader thread attempts to read (dequeues) a message from a bounded buffer.

·       A writer thread attempt to write (enqueues) a message to a bounded buffer.

·       A manager creates and starts a bunch of readers and writers that share a buffer.

Here's the design:

Buffers (version 1.0)

In this implementation the manager creates a buffer with capacity 3.

The manager also creates and starts 5 readers and 5 writers.

Each writer attempts to write 10 messages to the buffer.

Each reader attempts to read 10 messages from the buffer.

·       Message.java

·       Buffer.java

·       Reader.java

·       Writer.java

·       Manager.java

·       Utils.java

Here's the output produced:

·       output

Unfortunately, it's filled with failures and exceptions. In some cases the same message is received by multiple readers. Why?

Buffers (version 2.0)

Copy or import the buffers package given above into a new Eclipse project.

Without changing its capacity, modify Buffer so that all exceptions and failures are eliminated. In other words, no threads access the buffer at the same time. No writes fail, no reads fail. (A read fails if the buffer is empty and a write fails if the buffer is full.)