Re: Reader Writer monitor in JAVA

From: A. Bolmarcich (aggedor_at_earl-grey.cloud9.net)
Date: 03/29/05


Date: Tue, 29 Mar 2005 19:56:55 -0000

On 2005-03-29, Spliff Monkey <spliffmonkey@iname.com> wrote:
> 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?

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.

> If so how do I get around it.

You get around it by redesigning your solution so it does not deadlock.

> My code is here:
>
> http://www.maths.tcd.ie/~z/MainClass.java

In your code, your probably do not want the Start_Read method to use
the statement

  readers +=readers;



Relevant Pages

  • Reader Writer monitor in JAVA
    ... I am trying to program a simple monitor for a multiple reader single ... It seems as if, say, a consumer finishes its synchronized start_read. ... Then, say, a producer tries to write but is told to wait. ... Based in the following pseudo code for a monitor: ...
    (comp.lang.java.programmer)
  • Re: Storing Thread events in a Vector
    ... can't figure out how to get the Consumer thread to see the elements ... ProducerThread producer = new ProducerThread; ... String outputTime = format.format; ... You don't have any synchronization between your threads. ...
    (comp.lang.java.help)
  • Re: atomic operations...
    ... No MUTEXS! ... the Monitor class in .NET isn't strictly speaking a Windows mutex function). ... Until you've measured and confirmed a genuine performance problem, IMHO you should not care so much about the specific synchronization mechanism used. ... The Java implementation trades synchronization in the class itself for synchronization in the memory manager. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: pipes and multi-threading
    ... >> consumer and N other threads doing various work). ... > notification from one thread to another. ... > a lot more synchronization along the I/O path than you'll find ... >> can the kernel take advantage of that and reduce context switching (if there is ...
    (comp.unix.programmer)
  • Re: Book question on threads
    ... The thread is waiting on a monitor for an object so that it may access ... synchronization object, it can just go ahead and access the member ... synchronized int count; ... incrementCount is synchronized on total's monitor. ...
    (comp.lang.java.programmer)