This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes producer/consumer problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "producer/consumer problem" Watch "producer/consumer problem" New topic
Author

producer/consumer problem

tim thaler
Greenhorn

Joined: Sep 20, 2011
Posts: 1
following exercise:

5 producers baking cookies.
When at least 50 units in stock
then they inform the 2 consumers.
Consumers clear the stock.
When 100 units in stock, production stops
- use wait()/notify() concept
- use graphical output (applet/awt/swing)


okay guys,
please give me a little help





Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

You didn't say what your problems are
I don't think it is a good idea to start the thread itself in its constructor
Your for loop inside Producer iterate 101 times instead of 100
Your consumers should take those items from the stock?
When you have consumers clear the stock, "100 units in stock" should exclude those consumed?
Where did you use notify?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1077
    
  10

You should never update Swing components other than the EDT. Use SwingUtilities.invokeLater() instead.
You can read about Swing and concurrency here


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
I would add a display for the consumers, showing how many cookies they took. Also, I think I would have a display for produces and consumers showing the totals I would change the RIP to Stop or Quit. In the display to the console, you should add one to the producer number so that it matches the screen (5 instead of 4). It seems like you have the producer printing the total number of cookies after 50, but the produces should notify the consumer and the consumer should start taking cookies. This would reduce the number of cookies the producer has, except that there are 5 producers and only 2 consumers. Try this example.
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
Message deleted
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
Here's another attempt
1) Producers start right away, but stop when the total production reaches 100
2) Consumers start after there are 50 cookies, but stop when there are zero cookies.
3) Cntrl-C doesn't work.

Code is based on the example at
http://www.java-samples.com/showtutorial.php?tutorialid=306

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1077
    
  10

Wouldn't using Lock and Condition make it easier to do this?
 
 
subject: producer/consumer problem