Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Simple Producer/Consumer Thread Question

Reply
Thread Tools

Simple Producer/Consumer Thread Question

 
 
Buck Turgidson
Guest
Posts: n/a
 
      02-17-2004
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();
}
}


 
Reply With Quote
 
 
 
 
Michael N. Christoff
Guest
Posts: n/a
 
      02-17-2004

"Buck Turgidson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> 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



 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      02-17-2004

"Buck Turgidson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> 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.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      02-17-2004
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
 
Reply With Quote
 
Buck Turgidson
Guest
Posts: n/a
 
      02-17-2004
> 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.


 
Reply With Quote
 
Tony Dahlman
Guest
Posts: n/a
 
      02-21-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Simple GUI Updater Thread pek Java 12 11-02-2007 01:26 AM
Thread and wait simple question hilz Java 6 06-07-2005 05:56 AM
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
simple Thread question adeger Python 14 08-20-2004 08:02 PM
PyNew_Interpreter(): "Fatal Python error, invalid thread state for this thread" question vincent wehren Python 0 12-11-2003 08:09 PM



Advertisments