Simple Producer/Consumer Thread Question

Discussion in 'Java' started by Buck Turgidson, Feb 17, 2004.

  1. I am trying to get up to speed on threads, using Sun's tutorial. I think I
    got most everything, but what is confusing me is simply how the put() method
    (producer) ever breaks out of the while look and runs, because I don't see
    how the available flag is ever "true" for him. It seems like it will always
    be false, since the consumer exits get() leaving the flag set to false.

    Can someone clear up my confusion?



    public class CubbyHole {
    private int contents;
    private boolean available = false;

    public synchronized int get() {
    while (available == false) {
    try {
    wait();
    } catch (InterruptedException e) { }
    }
    available = false;
    notifyAll();
    return contents;
    }

    public synchronized void put(int value) {
    while (available == true) {
    try {
    wait();
    } catch (InterruptedException e) { }
    }
    contents = value;
    available = true;
    notifyAll();
    }
    }
    Buck Turgidson, Feb 17, 2004
    #1
    1. Advertising

  2. "Buck Turgidson" <> wrote in message
    news:...
    > I am trying to get up to speed on threads, using Sun's tutorial. I think I
    > got most everything, but what is confusing me is simply how the put()

    method
    > (producer) ever breaks out of the while look and runs, because I don't see
    > how the available flag is ever "true" for him. It seems like it will

    always
    > be false, since the consumer exits get() leaving the flag set to false.
    >
    > Can someone clear up my confusion?
    >
    >
    >
    > public class CubbyHole {
    > private int contents;
    > private boolean available = false;
    >
    > public synchronized int get() {
    > while (available == false) {
    > try {
    > wait();
    > } catch (InterruptedException e) { }
    > }
    > available = false;
    > notifyAll();
    > return contents;
    > }
    >
    > public synchronized void put(int value) {
    > while (available == true) {
    > try {
    > wait();
    > } catch (InterruptedException e) { }
    > }
    > contents = value;
    > available = true;
    > notifyAll();
    > }
    > }
    >
    >


    Well there must be an intial put() since CubbyHole does not have any initial
    data. At this point available=true. From here in, clients must interact
    with it in such a way that we get a sequence of alternating gets and puts.



    l8r, Mike N. Christoff
    Michael N. Christoff, Feb 17, 2004
    #2
    1. Advertising

  3. "Buck Turgidson" <> wrote in message
    news:...
    > I am trying to get up to speed on threads, using Sun's tutorial. I think I
    > got most everything, but what is confusing me is simply how the put()

    method
    > (producer) ever breaks out of the while look and runs, because I don't see
    > how the available flag is ever "true" for him. It seems like it will

    always
    > be false, since the consumer exits get() leaving the flag set to false.
    >
    > Can someone clear up my confusion?
    >
    >
    >
    > public class CubbyHole {
    > private int contents;
    > private boolean available = false;
    >
    > public synchronized int get() {
    > while (available == false) {
    > try {
    > wait();
    > } catch (InterruptedException e) { }
    > }
    > available = false;
    > notifyAll();
    > return contents;
    > }
    >
    > public synchronized void put(int value) {
    > while (available == true) {
    > try {
    > wait();
    > } catch (InterruptedException e) { }
    > }
    > contents = value;
    > available = true;
    > notifyAll();
    > }
    > }
    >


    This example shows a cubby hole that is either empty (message not available)
    or has one message in it (message available.) For the get method, if
    there's nothing in the cubby hole (available is false), it waits until a
    message is available (available is true), but then it removes the message
    and sets available back to false.

    Put is the reverse--available must be false for it to proceed. If available
    starts false, the loop is skipped and the value goes straight into the cubby
    hole and available becomes true. However, if there is already a message in
    the cubby hole (available is true) put waits until some other thread removes
    the message. There must be another thread that gets the message or the put
    will wait forever. As soon as there is no message (available is false --
    loop ends) the put puts the new message in the cubby hole, which makes
    available true again.

    Cheers,
    Matt Humphrey http://www.iviz.com/
    Matt Humphrey, Feb 17, 2004
    #3
  4. On Tue, 17 Feb 2004 15:23:36 GMT, Buck Turgidson wrote:
    > I am trying to get up to speed on threads, using Sun's tutorial. I
    > think I got most everything, but what is confusing me is simply how
    > the put() method (producer) ever breaks out of the while look and
    > runs, because I don't see how the available flag is ever "true" for
    > him. It seems like it will always be false, since the consumer exits
    > get() leaving the flag set to false.
    >
    > Can someone clear up my confusion?


    Consider what might happen if the producer attempts to call put()
    twice in quick succession, without an intervening get(). In the second
    call to put(), available is already true and the producer is forced to
    wait until a consumer retrieves the data that was recently inserted.
    This prevents the existing data from being overwritten.

    Note that you might just as well have asked about get(), where
    available always seems to be true (since the producer always leaves it
    that way). But if get() is invoked without an intervening put(), the
    consumer is forced to wait until new data arrives before he is allowed
    to retrieve, so that the same data isn't returned more than once.

    Either of these cases could occur when the producer and consumer don't
    have equal amounts of work to do, or when there are more than one of
    each.

    In other words, available is used to force a strict interleaving
    between calls to put() and get().

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 17, 2004
    #4
  5. > This example shows a cubby hole that is either empty (message not
    available)
    > or has one message in it (message available.) For the get method, if
    > there's nothing in the cubby hole (available is false), it waits until a
    > message is available (available is true), but then it removes the message
    > and sets available back to false.
    >
    > Put is the reverse--available must be false for it to proceed. If

    available
    > starts false, the loop is skipped and the value goes straight into the

    cubby
    > hole and available becomes true. However, if there is already a message

    in
    > the cubby hole (available is true) put waits until some other thread

    removes
    > the message. There must be another thread that gets the message or the

    put
    > will wait forever. As soon as there is no message (available is false --
    > loop ends) the put puts the new message in the cubby hole, which makes
    > available true again.


    Thanks to all. It's clear now.
    Buck Turgidson, Feb 17, 2004
    #5
  6. Buck Turgidson

    Tony Dahlman Guest

    Buck Turgidson wrote:
    >
    > Thanks to all. It's clear now.


    But you can do better, Buck. You can add or modify code from
    that example to create a little bit bigger cubby hole, one that
    takes a whole bunch of inputs before it has a chance to output
    anything.

    The Sun example is really a queue if you look at it. You could
    write code that expands on it and takes "x" number of inputs
    before the queue is full (after which it still shuts off input
    in just the way that the Sun example does).

    Try it. If you have trouble, you can look at my version of solving
    this problem at:

    http://pws.prserv.net/ad/programs/Programs.html#FIFO

    Good luck! Regards, Tony Dahlman
    ---------------------------------------
    nospam, I've had it, thanks.
    Tony Dahlman, Feb 21, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McKay
    Replies:
    0
    Views:
    435
    Mark McKay
    Dec 9, 2003
  2. Usenet Poster!!!
    Replies:
    4
    Views:
    1,794
    Eric Sosman
    Sep 30, 2004
  3. Jeff
    Replies:
    4
    Views:
    662
    xarax
    Oct 22, 2004
  4. Replies:
    0
    Views:
    1,068
  5. Replies:
    3
    Views:
    1,484
Loading...

Share This Page