Simple Producer/Consumer Thread Question

B

Buck Turgidson

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();
}
}
 
M

Michael N. Christoff

Buck Turgidson said:
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
 
M

Matt Humphrey

Buck Turgidson said:
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 (e-mail address removed) http://www.iviz.com/
 
G

Gordon Beaton

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
 
B

Buck Turgidson

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.
 
T

Tony Dahlman

Buck said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top