Reader Writer monitor in JAVA

Discussion in 'Java' started by Spliff Monkey, Mar 29, 2005.

  1. I am trying to program a simple monitor for a multiple reader single
    writer problem in JAVA based on the pseudo code given at the bottom of
    this post. A link to my code is also given bellow.

    I get deadlock. I basically have producer and consumer classes which
    do nothing except call the Start_Read/Write and End_Read/Write from
    the monitor. I also have created a condition class.

    It seems as if, say, a consumer finishes its synchronized start_read.
    Then, say, a producer tries to write but is told to wait. Then the
    consumer just stops and never enters its end_read.

    The consumer should enter the end_read and then signal the producer.

    Does the producer waiting in the synchronized start_write, prevent the
    consumer entering the synchronized end_read?

    If so how do I get around it.

    My code is here:

    Based in the following pseudo code for a monitor:

    monitor Reader_Writer_Monitor is
    Readers: Integer := 0;
    Writing: Boolean := False;
    OK_to_Read, OK_to_Write: Condition;

    procedure Start_Read is
    if Writing or Non_Empty(OK_to_Write) then
    end if;
    Readers := Readers + 1;
    end Start_Read;

    procedure End_Read is
    Readers := Readers - 1;
    if Readers = 0 then Signal(OK_to_Write); end if;
    end End_Read;

    procedure Start_Write is
    if Readers /= 0 or Writing then
    end if;
    Writing := True;
    end Start_Write;

    procedure End_Write is
    Writing := False;
    if Non_Empty(OK_to_Read) then
    end if;
    end End_Write;
    end Reader_Writer_Monitor;
    Spliff Monkey, Mar 29, 2005
    1. Advertisements

  2. Yes. In the sequence of events that you described, the Start_Read()

    is synchronized on its Monitor object
    invokes OK_to_Read.wait_() which
    is synchronized on its Condition object (OK_to_Read)
    invokes wait on its Condition object

    This wait releases the synchronization lock on the Condition object; it
    does not also release any other synchronization locks the thead has.
    You get around it by redesigning your solution so it does not deadlock.
    In your code, your probably do not want the Start_Read method to use
    the statement

    readers +=readers;
    A. Bolmarcich, Mar 29, 2005
    1. Advertisements

  3. Maybe you want to study Sun's Producer/Consumer example in

    and extend it as necessary.

    Thomas Weidenfeller, Mar 30, 2005
  4. This wait releases the synchronization lock on the Condition object; it
    How do I get it so that when the producer waits it releases the lock
    so that end_read may execute?

    Yeah, that should be readers+=1;

    Thank you for your time
    Spliff Monkey, Mar 30, 2005
  5. The writer cannot wait on multiple locks. You need to avoid having the
    writer wait on one lock while holding another lock that the consumer
    needs to proceed and release the writer.

    for one approach.
    A. Bolmarcich, Mar 30, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.