tag:blogger.com,1999:blog-8712770457197348465.post8379818278416374862..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: Producer Consumer Design Pattern with Blocking Queue Example in Javajavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger26125tag:blogger.com,1999:blog-8712770457197348465.post-87263600487726871622018-09-16T05:22:12.594-07:002018-09-16T05:22:12.594-07:00Hi,
Is this implementation of producer consumer is...Hi,<br />Is this implementation of producer consumer is thread safe?<br />What if there are multiple producer and multiple consumer and if we are using linkedblockingqueue for sharing of the data , will it not cause data consistency issue ?Anonymoushttps://www.blogger.com/profile/04896157912994906000noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17227368119670754992016-07-11T08:22:20.953-07:002016-07-11T08:22:20.953-07:00Hello @Amol, could you please care to explain your...Hello @Amol, could you please care to explain your example for beginners, and what is difference between original solution and this one? Cheersjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-88692007929686098782016-07-11T00:58:46.509-07:002016-07-11T00:58:46.509-07:00//package threadexample;
class ProdConsu implement...//package threadexample;<br />class ProdConsu implements Runnable{<br /> public static int count=0;<br /> Thread t1=new Thread(this);<br /> Thread t2=new Thread(this);<br /> Thread t3=new Thread(this);<br /> int i=0;<br /> public ProdConsu ()<br /> {<br /> t1.start();<br /> t2.start();<br /> t3.start();<br /> }<br /> public void run(){<Anonymoushttps://www.blogger.com/profile/12203382195601931276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38740514997152202262016-05-07T10:28:22.199-07:002016-05-07T10:28:22.199-07:00Hi Javin, Thank you very much for this wonderful a...Hi Javin, Thank you very much for this wonderful article and explaining it in a simple way.<br /><br />I just want to point out that there is no "ArrayLinkedQueue" in java and it should be replaced with "ArrayBlockingQueue", please see "ArrayLinkedQueue is bounded in nature" text in an article.<br /><br />/ChiragAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10055841005981419902016-05-07T10:23:42.723-07:002016-05-07T10:23:42.723-07:00Hi Javin, Thank you very much for this wonderful a...Hi Javin, Thank you very much for this wonderful article and explaining it in a simple way.<br /><br />I just want to point out that there is no "ArrayLinkedQueue" in java and it should be replaced with "ArrayListQueue", please see "ArrayLinkedQueue is bounded in nature" text in an article.<br /><br />/ChiragAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-51726913607363965902016-03-05T18:54:48.248-08:002016-03-05T18:54:48.248-08:00@Javin Paul: Thanks, it worked. @Javin Paul: Thanks, it worked. Newhttps://www.blogger.com/profile/13263944561649619278noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-53843830775504700122016-03-04T19:14:50.768-08:002016-03-04T19:14:50.768-08:00@New, your syntax of using wait and notify is inco...@New, your syntax of using wait and notify is incorrect. You should check the condition like queue.isEmpty() or queue.size() == size on while loop, not in if block due to reasons mentioned <a href="http://javarevisited.blogspot.com/2013/12/inter-thread-communication-in-java-wait-notify-example.html" rel="nofollow">here</a>. You can also see my implementation of producer consumer using wait notifyjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43649057944266024992016-03-04T14:18:16.040-08:002016-03-04T14:18:16.040-08:00I am trying to implement producer consumer problem...<br />I am trying to implement producer consumer problem using wait and notify. The problem is that, the producer is first producing everything and then consumer starts consuming. I am new to multithreaded environment.<br /><br />Please find below my code:<br /><br />package javaExamples;<br /><br />import java.util.ArrayList;<br />import java.util.List;<br /><br />public class Newhttps://www.blogger.com/profile/13263944561649619278noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32816961797604734922016-02-20T08:26:45.768-08:002016-02-20T08:26:45.768-08:00An simple example with two consumers.
http://simp...An simple example with two consumers.<br />http://simpleapphub.blogspot.in/2016/02/simple-producer-consumer-framework-in.html<br /><br />Anonymoushttps://www.blogger.com/profile/07261005437787606288noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76902641120821760582016-01-21T01:48:46.638-08:002016-01-21T01:48:46.638-08:00Can someone solve this?
Consider a single produce...Can someone solve this?<br /><br />Consider a single producer and 10 consumers as threads. The consumers has to subscribe (Ad themselves) to the producer in-order to get any messages that produces sends. <br /><br />The expected functionality is that, the producer should add a message to a data structure/collection (you choose of your choice) every 2 minutes and notify all consumers. When the Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73986015537692130572015-12-29T23:05:03.430-08:002015-12-29T23:05:03.430-08:00The simplest way to solve the producer consumer pr...The simplest way to solve the <a href="http://java67.blogspot.com/2012/12/producer-consumer-problem-with-wait-and-notify-example.html" rel="nofollow">producer consumer problem</a> is by using Blocking-queue, as explained in this <a href="http://java67.blogspot.com/2015/12/producer-consumer-solution-using-blocking-queue-java.html" rel="nofollow">tutorial</a>, but if you really want to understand Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14528721436109983072015-09-20T09:50:18.299-07:002015-09-20T09:50:18.299-07:00Hello
In the above example in the producer code we...Hello<br />In the above example in the producer code we have to r put the following after put otherwise ordering is not guaranteed.<br /><br /> Thread.sleep(1000);Srinivashttps://www.blogger.com/profile/07847083908709272156noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27670571856660987922015-09-19T03:35:17.248-07:002015-09-19T03:35:17.248-07:00it give wrong result .If you print vector to know ...it give wrong result .If you print vector to know status of vector each time after consume or produce method then we can see than vector grow above given limit 10.Anonymoushttps://www.blogger.com/profile/09998734811630075172noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28075463949917532272015-08-22T20:59:39.451-07:002015-08-22T20:59:39.451-07:00@Anonymous, yes, consumer is running in infinite l...@Anonymous, yes, consumer is running in infinite loop, as is mostly case in real world and I kept producer down to 10 messages just for demo.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44553840347059543122015-08-22T18:07:23.574-07:002015-08-22T18:07:23.574-07:00With the given code, the Consumer seems to be in a...With the given code, the Consumer seems to be in an infinite loopAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29715899657399737592015-07-27T08:56:14.875-07:002015-07-27T08:56:14.875-07:00Javin,
In your example the program will never exi...Javin,<br /><br />In your example the program will never exit and @Gaurav is trying to break the program after 10 iteration.<br />please let us know your thoughts.<br /><br />Thanks, <br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16487142694586506322015-07-13T08:52:50.643-07:002015-07-13T08:52:50.643-07:00@Gaurav, what was the problem and how did you fixe...@Gaurav, what was the problem and how did you fixed it?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56518072605377205912015-07-12T11:51:45.269-07:002015-07-12T11:51:45.269-07:00seems like your method ain't right.
how about ...seems like your method ain't right.<br />how about this son:<br />import java.util.concurrent.*;<br /><br />class Producer implements Runnable {<br /> <br /> private BlockingQueue queue;<br /> <br /> Producer(BlockingQueue q){<br /> this.queue=q;<br /> }<br /> <br /> public void run() {<br /> //produce messages<br /> for(int i=1; i<11; i++){<br />Gouravhttps://www.blogger.com/profile/07650612181243410422noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68053275611907850902014-05-22T04:58:30.269-07:002014-05-22T04:58:30.269-07:00package Thread;
import java.util.concurrent.Semap...package Thread;<br /><br />import java.util.concurrent.Semaphore;<br /><br />class SharedResource<br />{<br /> int n;<br /> static Semaphore semCons = new Semaphore(0);<br /> static Semaphore semProd = new Semaphore(1);<br /> <br /> void put(int n)<br /> {<br /> try {<br /> semProd.acquire();<br /> } catch (InterruptedException e) {<br /> // TODO Auto-generated catch block<br /> sathishhttps://www.blogger.com/profile/16548176154083945507noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-7673149163643964872014-02-13T21:51:08.652-08:002014-02-13T21:51:08.652-08:00Can you please share solution of Producer Consumer...Can you please share solution of Producer Consumer problem using Semaphore? I know it can be solved using multiple way including BlockingQueue, wait and notify as shown above, but I am really interested in using Semaphore. ThanksAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24028311478286288972013-12-01T01:26:28.369-08:002013-12-01T01:26:28.369-08:00Recently I got this question in interview with dif...Recently I got this question in interview with different scenario. How to resolve the producer and consumer problem so that my CPU cycle can be used to 100%. For ex if producer is producing less and consumer is consuming fast then your CPU cycle is getting wasted which is associated with cost. So what would be strategy to resolve this. Any suggestion?<br />====================<br />(Count of Anonymoushttps://www.blogger.com/profile/14371581858849848974noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64854311937969748212013-11-05T12:06:09.768-08:002013-11-05T12:06:09.768-08:00I tried out following implementation of Producer-C...I tried out following implementation of Producer-Consumer and it doesn't list out all the elements from the list. Few of them are missing. <br /><br />I wanted to have one producer & multiple consumers and have the consumers take data out of the list in round robin fashion, but not working as expected. Would you be able to point me out the error in the code? Thanks.<br /><br />public Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-25882462331707084022013-10-08T20:30:21.967-07:002013-10-08T20:30:21.967-07:00The above example is good but too simple. Would yo...The above example is good but too simple. Would you minding providing a example with multiple producers and consumers and stopping the program once producers have done their jobs?yychttps://www.blogger.com/profile/01699762369204618098noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72033812765850698062013-04-13T21:26:24.683-07:002013-04-13T21:26:24.683-07:00Hi Javin, ..gr8 article few things that I want to ...Hi Javin, ..gr8 article few things that I want to add in this is... <br /><br />BlockingQueue Code Example<br /><br />Here is an example of how to use a BlockingQueue. The example uses the ArrayBlockingQueue implementation of the BlockingQueue interface.<br /><br />First, the BlockingQueueExample class which starts a Producer and a Consumer in separate threads. The Producer inserts strings into aSARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37491124586241977332013-03-07T09:06:22.971-08:002013-03-07T09:06:22.971-08:00Well i see something is missing, how to define the...Well i see something is missing, how to define the size of queue ?Mudit Srivastavahttps://www.blogger.com/profile/03961194831826056132noreply@blogger.com