This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

producer/consumer problem

 
tim thaler
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never update Swing components other than the EDT. Use SwingUtilities.invokeLater() instead.
You can read about Swing and concurrency here
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Message deleted
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't using Lock and Condition make it easier to do this?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic