tag:blogger.com,1999:blog-8712770457197348465.post503011452938291921..comments2024-03-28T23:53:37.044-07:00Comments on Javarevisited: How to use wait, notify and notifyAll in Java - Producer Consumer Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger28125tag:blogger.com,1999:blog-8712770457197348465.post-36356095192102301952023-02-04T00:13:53.184-08:002023-02-04T00:13:53.184-08:00Hello Anonymous, its good that you ask as lot of p...Hello Anonymous, its good that you ask as lot of people don't understand this concept but don't ask due to fear of looking bad, which is not correct, you should ask no matter how simple the question. Coming back to your question, <br /><br />What exactly notify and notifyAll() do?<br />When a thread called wait() on an object like myObject.wait(), they will stop right there and will not javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15434048323105522472023-02-01T07:35:21.120-08:002023-02-01T07:35:21.120-08:00Hi. Thank you very much for your detailed explanat...Hi. Thank you very much for your detailed explanation. You put great emphasis on important things. Still unfortunately I could not get what exactly notify() and notifyAll() do? What kind of notification do they send? Is it that they just make the code quit the synchronized block while we still remain in infinite loop? Then how and when we enter synchronized block again? And another problem not Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16129779190614216542019-09-11T11:00:03.579-07:002019-09-11T11:00:03.579-07:00what a fantastic clear explanation thankswhat a fantastic clear explanation thanksbiniamhttps://www.blogger.com/profile/15038421682464095584noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-23771471760989879932018-03-06T20:14:36.158-08:002018-03-06T20:14:36.158-08:00I have to agree with Piyush Partap
"In above ...I have to agree with Piyush Partap<br />"In above example, consumer comes into picture only when the producer has filled up the queue upto its max size. Similarly, the producer starts producing only after the queue is fully consumed. This should not be the case.<br />The concept that this problem is based on, is that the producer and consumer should be able to work separately. That means theAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4259016339385299552018-02-21T19:26:12.706-08:002018-02-21T19:26:12.706-08:00superb post, Thanks a tonsuperb post, Thanks a tonAnonymoushttps://www.blogger.com/profile/16174242354676870714noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-8577639302049235542017-08-02T06:27:02.223-07:002017-08-02T06:27:02.223-07:00Hello Anonymous, actually notify() is outside loop...Hello Anonymous, actually notify() is outside loop. Idea is you check for a condition and if condition is for waiting then you wait e.g. if bucket is empty then wait, if not then its time to consume items and then notifying producer thread which might be waiting because queue was full. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18523123325772860482017-08-01T11:02:06.059-07:002017-08-01T11:02:06.059-07:00Hi Javin, Why can't we write notifyAll() right...Hi Javin, Why can't we write notifyAll() right after queue.wait()?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-5340242334651007772017-05-10T03:02:05.075-07:002017-05-10T03:02:05.075-07:00Hello @Anonymous, what is the problem? Can you ple...Hello @Anonymous, what is the problem? Can you please tell us bit more?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22896085833576262072017-05-09T23:43:01.138-07:002017-05-09T23:43:01.138-07:00Program is not proper. Its executing Producer and ...Program is not proper. Its executing Producer and Consumer at the same time.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64852361255232904332017-05-03T16:51:38.047-07:002017-05-03T16:51:38.047-07:00Hello @munjal, this is to ensure that code should ...Hello @munjal, this is to ensure that code should work fine with just one producer/consumer or multiple producers and consumers. You are right in the sense that in our program we have just one producer and consumer and we can definitely use notify() instead of notifyAll() but our code doesn't prevent anything from creating more than one producer or consumer, if you use notify, then that can javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50526449637009998422017-05-03T07:31:47.915-07:002017-05-03T07:31:47.915-07:00why you have user notifyAll() here ? we have only ...why you have user notifyAll() here ? we have only two threads here which are interdependent.munjalhttps://www.blogger.com/profile/18283210312291269744noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-8708680814532570292017-03-09T17:29:04.585-08:002017-03-09T17:29:04.585-08:00Hello Munjal, instead of writing code using wait a...Hello Munjal, instead of writing code using wait and noitfy, you should just use BlockingQueue, you don't have to synchronize anything, Queue will ensure that consumer threads will wait if queue is empty and producer threads will wait if queue is full. This can support multiple producer and consumer as well. See <a href="http://www.java67.com/2015/12/javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47415487854326981312017-03-09T08:46:17.442-08:002017-03-09T08:46:17.442-08:00Hello sir,
Thank you for publishing such a great a...Hello sir,<br />Thank you for publishing such a great article.<br />I am new to java world, and I really like your post.<br /><br />Here we have taken only one producer thread and one consumer thread.<br />In my application , I need to create more than one producer and consumers.<br />In such cases I can not write such a big synchronized block , as if producer-1 is creating Item-1 , producer-2 munjalhttps://www.blogger.com/profile/18283210312291269744noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-52240457261750233972017-01-22T15:29:08.282-08:002017-01-22T15:29:08.282-08:00Why adding item to queue in produce and removing i...Why adding item to queue in produce and removing item from queue in consume not in synchronised block (along with the notifies)? I think it should have been. Sanghmitrahttps://www.blogger.com/profile/12994121703758959749noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-79099203546844248072016-10-09T13:09:50.606-07:002016-10-09T13:09:50.606-07:00Very nice post!! Well presented. Thanks.Very nice post!! Well presented. Thanks.Tasneem Hyderhttps://www.blogger.com/profile/06057172733100627569noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90091708253865731252016-08-03T12:12:02.344-07:002016-08-03T12:12:02.344-07:00When there is many producer and consumers it might...When there is many producer and consumers it might cause race condition. Let's assume there is 2 consumer and 1 producer. With bad luck 2 producers call remove and there is check then act and NoSuchElementException is thrown. Anonymoushttps://www.blogger.com/profile/18359002564532604419noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-42225096554383827952016-08-02T10:40:08.588-07:002016-08-02T10:40:08.588-07:00i have tried the above problem with multiple produ...i have tried the above problem with multiple producers and consumers . so, i am really confused how can we work on that one.Sumithttps://www.blogger.com/profile/02952896249363094004noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-19673216480109210222016-08-01T07:49:24.739-07:002016-08-01T07:49:24.739-07:00Producing value ..2145701772
Producing value ..-15...Producing value ..2145701772<br />Producing value ..-1583439136<br />Producing value ..-1496055580<br />Producing value ..1680454426<br />Producing value ..-755744647<br />Producing value ..-2092974764<br />Producing value ..880553434<br />Producing value ..62086747<br />Producing value ..1833223884<br />Producing value ..2079999503<br />List is full ..<br />List is full ..<br /><br />This is Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65236316231275927102016-06-20T11:48:30.683-07:002016-06-20T11:48:30.683-07:00very useful post.. Thanksvery useful post.. ThanksAnonymoushttps://www.blogger.com/profile/11745123242597968110noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33890174670533247942016-05-29T18:22:27.284-07:002016-05-29T18:22:27.284-07:00Good post Thanks!!Good post Thanks!!Anonymoushttps://www.blogger.com/profile/08147074394836305194noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70895349121470871342016-04-30T12:44:33.846-07:002016-04-30T12:44:33.846-07:00Brilliant. Thanks!Brilliant. Thanks!Sidakhttps://www.blogger.com/profile/02781988830072324237noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-74979251858212420272016-01-17T18:05:22.733-08:002016-01-17T18:05:22.733-08:00The code works correctly as Javin says. The reason...The code works correctly as Javin says. The reason is that the queue.wait() RELEASES the intrinsic lock so that the other thread can modify the queue object (enqueue or dequeue).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83654010072250235002016-01-15T22:37:07.686-08:002016-01-15T22:37:07.686-08:00Hi, that's not true. Both Producer and Consume...Hi, that's not true. Both Producer and Consumer are running in parallel. If your assumption is based upon the output than it's wrong because threads are running really fast, try introducing sleep after adding and removing elements from linkedlist (the shared queue) and debug the program in Eclipse. you will see both threads are working in parallel, waiting for locks, check condition and javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-41203517607729550492016-01-15T00:27:47.948-08:002016-01-15T00:27:47.948-08:00In above example, consumer comes into picture only...In above example, consumer comes into picture only when the producer has filled up the queue upto its max size. Similarly, the producer starts producing only after the queue is fully consumed. This should not be the case.<br />The concept that this problem is based on, is that the producer and consumer should be able to work separately. That means the consumer is free to start consuming once the ρєєуυѕн ρяαтαρhttps://www.blogger.com/profile/15656434467592543297noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-91040024747694574762015-12-29T23:06:12.309-08:002015-12-29T23:06:12.309-08:00The simplest way to solve the producer consumer pr...The simplest way to solve the producer consumer problem 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 the core concept behind producer consumer pattern then you must solve this problem using <a href="http://java67.blogspot.com/Anonymousnoreply@blogger.com