• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Producer Consumer Problem  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am having a query in my code below.. If for some reasons when producer finished producing the item and notifies all threads and at that time the consumer threads were not in waiting state then the item would be lost... Any ideas how to avoid this problem is appreciated... I feel its like a race condition..
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:Am having a query in my code below.

kushi,

Please DontWriteLongLines.
It makes your thread very hard to read, and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

Oh, and you're usually better off using spaces for indentation, rather than TABs.

Thanks.

Winston
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Winston-->Will definitely follow from next time.. and thanks for this time..
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:Am having a query in my code below.. If for some reasons when producer finished producing the item and notifies all threads and at that time the consumer threads were not in waiting state then the item would be lost...

In your code, I don't think that can happen. Your producer could produce before the consumers are ready, but the consumers won't miss it. Your consumer checks the value stored in the Good and only wait()s if there is no value. The producer sets a value then notifyAll()s. If the producer gets the lock first, it will set the value, notifyAll() and come to an end. Then the first consumer will get the lock, check the value and see it isn't empty and so won't wait(), it will just report and end. Then the second consumer will get the lock, see the value isn't empty (because your consumer doesn't actually consume, it just views) and won't wait(), it will get the value and report. Same with the third.

But in general, if you had a producer that produced more than one value, then yes, you could produce values that the consumers never see (example, producer gets the lock twice in a row without an intermediary consumer having a chance at it). To avoid this you either put the values in a collection (A BlockingQueue implementation works nicely most of the time) or you make the producer wait after it produces a value until it is consumed. That means adding a signal where the producer wait()s and the consumer notify()s. That way the producer never sends a new value until the previous one was consumed.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kushi kumar wrote:If for some reasons when producer finished producing the item and notifies all threads and at that time the consumer threads were not in waiting state then the item would be lost...

It all depends on what this Producer→Consumer model is supposed to look like. Is it finite (like a Queue), or inifinite (like a Pool).

Any ideas how to avoid this problem is appreciated... I feel its like a race condition...

There are almost invariably race conditions involved in multi-threading - in fact, it's the basic definition of the problem. Your job is to make sure (usually by synchronization) that no more than one Thread can access any "raced for" object (or variable) at once. And strangely enough, the key to that is keeping synchronized methods as short and simple as you possibly can.

Question: What are you trying to do?

Winston
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Steve-Thanks... Bang on...

Using collection, I did try one sometime back...
http://www.coderanch.com/t/620272/java/java/Producer-Consumer-Java

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!