Solution to the Readers Writers problem

A multithreaded JAVA program, named readerwriter, that simulates the behavior of a fixed number of readers and writers that share a single resource. Four readers and four writers are used.

Synchronization among the threads is done as below:

  • Readers may share the resource non-exclusively, but writers must use the resource exclusively.
  • Any reader may start using the resource as long as there is no writer currently using it.
  • Readers must wait if there is any writer that has arrived in the waiting queue earlier. But, they don’t have to wait for the writer that arrive after them.
  • A Reader can use the resource while other readers are currently using it.
  • Writers must wait until all the readers or the writers currently using the resource release it.

Each reader thread will be numbered from 0 to 3. So are the writer threads.

Class scheduler: implements the synchronized methods, startread, endread, startwrite,and endwrite.

Class timer: implements a method, pause, that delays a thread for a random interval between 3000 and 6000 msec.

Class readerx: implements a reader thread that terminates after calling pause, startread, pause, and endread, in that order.

Class writerx: implements a writer thread that terminates after calling pause, startwrite,pause, and endwrite, in that order.

Class readerwriter: defines a main method that starts 4 readers and 4 writers and waits until these 8 threads have terminated.

[The above code is not guaranteed to be right]

Creative Commons License Valid CSS Valid XHTML 1.0 Driven by DokuWiki Powered by PHP Powered by Apache get firefox!! kgareth.com Recent changes RSS feed