Java Producer Consumer Program
Posted : adminOn 6/13/2018You could tell us how you expect your program to behave. Do you mean your producer and consumer threads need to be synchronized?
In this post we will see Java program for producer consumer problem.This is an important Java interview question in Thread. In computing, the producer–consumer problem (also known as the bounded-buffer problem). // Java program to implement solution of producer // consumer problem. Geektool Uninstall For Mac.
Sep 21, 2015 Producer Consumer Problem with Wait and Notify >Java ProgramOperating Systemsimport java.util.Vector;import java.util.logging.Level;import java.util.
If yes, then what kind of synchronization are you looking for? Are you ok if the get thread misses a couple of values set by the other thread, i.e you just want that each of these threads taken separately behaves in a consistent way as regards the shared object-- this is relatively simple. Or do you also want that the updates to the state of the shared object are also not missed - in which case you might need to have some custom wait, notification logic build up. Also it seems, you aren't new to ranch. You might want to consider using the code tags to make your question readable. Also unless you don't have a strong reason to not follow Java naming conventions, I believe you might want to consider changing your class names and method names. Thanks, Chan.
V K Gupta wrote:please reply Hi V K Gupta, You posted this all of four minutes after your previous post. The good folks on this forum are volunteers and have their own things to do: they aren't here to read and respond to your posts within seconds of your posting. Even if they were, allowing just four minutes between prods hardly gives time to understand the code you posted an provide a detailed response (after all if it were that easy you probably wouldn't have needed the help, you are clearly bright, so what is hard for you is not going to be easy for anyone else). So for next please (. Greetings, Steve.
Thanks so much for such an informative and constructive feedback. I greatly appreciate it. Based on your suggestions, I have reworked on my code to take care of point 1 ( what if consumer gets the lock first ) by having an extra condition in the run method of the Consumer that can issue an extra notify() if that condition is met. I'm not sure if this is a graceful solution to handle the issue-- it's like too many conditions based on hard coded data values which makes your system difficult to be reused ( Just a guess). I'd like to post my solution here so I could (hope to) get a feedback again but I can't steal this post from the OP who reserves the first right to work on it and may have a better solution than mine.
So I'll wait for a while.:-) You almost provided us with the solution for point 2 ( nested locks ). So this part was easy.
BlockingQueue description provided by you seems like a very pretty solution. It seems like a nice advanced construct in Java that I'd like to cover as soon as I can. Once I've studied about it, I will come back to this post to implement this requirement ( point 1 might become very easy then ) using a BlockingQueue.
Thanks, Chan. Hi Steve, First things first. Thanks so much. It means a lot to me to have you ( and others too when they do it ) go through my code and provide such a nice feedback along with help on how to resolve the problems in my code and explaining why another approach is better.
I greatly appreciate it. I will work on it and try to not have any new mistakes in my code.:-) About putting the Poison Pill back into the queue -- Every time I've worked on a BlockingQueue implementation where there were multiple consumers, I made producers produce multiple Poison Pill value. Such a bad design ( I know )! I had thought about it multiple times but could not come up with the idea of putting the Poison back into the queue. Once I vaguely considered putting the posion back into the queue but I was too quick to discard the idea ( without any reason ) even before it could register. Now that you've showed it to me. I'm going to work on it with all those wonderful suggestions.
Thanks and greetings, Chan. Xilinx Coe File Matlab here.