• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

producer/consumer problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Message deleted
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't using Lock and Condition make it easier to do this?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic