• Post Reply Bookmark Topic Watch Topic
  • New Topic

Producer_Consumer in Java  RSS feed

 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried a Producer Consumer program.. Sometimes the output is correct, sometimes exception occurs in middle of output although the program runs to completion..
I get IndexOutOfBoundsException and I believe the reason being the following:- when the Q is empty, all the 3 consumer threads go into waiting state; when the producer adds an item and notifies all the waiting threads, after a consumer thread removes the item,the other consumer thread waking up will try to remove(when the Q is now empty) which causes this problem.. I know its a race condition but not able to figure out how to avoid it... Any ideas/suggestions are welcome..

Another query - Am not able to figure out a way to gracefully terminate this program.. As of now I have used System.exit(0) when the last item is produced.. Any other better ideas are welcome..

P.S
----
I don't want to use any java's API synchronized classes, I wanted try using wait()/notify() mechanism..


Queue Class
 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually there are quite a few similar threads here on the same subject. You could search for them. Although not exactly similar to your case, those discussions may give you valuable insights.

Coming to your problem, one of the thing that is incorrect with your code is the following part. If you look well, you'd realize that you're not producing 49 though it gets printed. You might want to do some rearrangement of instructions there so you display exactly what the producer produces.



Also for how long to produce and when to stop producing is an aspect specific to a particular producer. You may want to remove that logic altogether from the Queue class and have it in your Producer class, although differently ( explained below ).
To address the case of how to exit, you could use the concept of a Poison Pill. It's not a valid value to be produced/consumed but it is something that you could have the Producer and Consumers test for. Like you could have the producer and consumer constructor accept an Integer Object like this.


In the producer code, you could produce this value as the last value. The consumer code could check if the value is equal to ( use == here ) the killSignal and then it can stop. Since you have multiple consumers, you may want to add the value back so other consumers can again consume this value.

Few days back I was also working on a similar problem and Steve had suggested me a very elegant enum approach ( also this approach is his suggestion ). I liked it a lot. If you like you can search for it in the forum Threads and Synchronization.

Hope it helps.
Chan.

Edit : Since you have multiple consumers, the enum approach might not be handy here, though I admit I haven't thought deeply about it ( I will, later :-) ).
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I think it will be better to use use ReentrantLock and Condition here because producer is waiting for a different event and there are multiple consumers waiting for a different event. I have not yet analyzed whether using synchronized methods and notifyAll and notify can create an incorrect case here cause I'd really have to think deep on that. But if you don't want to analyze that thing and if you want to use a safer mechanism, locks and conditions are better for this case ( I think so, unless someone corrects me ).
 
kushi kumar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:
Coming to your problem, one of the thing that is incorrect with your code is the following part. If you look well, you'd realize that you're not producing 49 though it gets printed. You might want to do some rearrangement of instructions there so you display exactly what the producer produces.


I think the producer produces 49 items..
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!