tag:blogger.com,1999:blog-8712770457197348465.post5722310540347195761..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: How to use Lock and Condition variable in Java? Producer Consumer Problem Example Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-8712770457197348465.post-70448442556131565832021-02-21T06:06:20.845-08:002021-02-21T06:06:20.845-08:00I have a question as to why two condition variable...I have a question as to why two condition variables are used? I have coded this using a single condition variable and it works fine. (This is essentially the way synchronized/wait/notifyAll works.) Are two condition variables used for clarity or some other reason?<br /><br />Thanks,<br />Bob E.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-82003939326825984602019-06-21T07:40:03.544-07:002019-06-21T07:40:03.544-07:00Thank you. I was searching for this example for ho...Thank you. I was searching for this example for hours.Raghavendra T.Ahttps://www.blogger.com/profile/05499986495809437161noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16033966741191746382017-08-21T22:03:38.995-07:002017-08-21T22:03:38.995-07:00ReentrantReadWriteLock is not an implementation of...ReentrantReadWriteLock is not an implementation of Lock interface.It is a implementation of ReadWriteLock InterfaceAnonymoushttps://www.blogger.com/profile/06251261975594817262noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45454589358611357002016-05-22T13:56:16.814-07:002016-05-22T13:56:16.814-07:00are the variables buffernotfull and buffer not emp...are the variables buffernotfull and buffer not empty switched here? why will producer sings, buffer not full after adding element. it should signal buffer not empty right?Anonymoushttps://www.blogger.com/profile/18069207085669028146noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71053286720175876422015-12-20T03:10:41.755-08:002015-12-20T03:10:41.755-08:00Thanks for the reply.
Consider this scenario with ...Thanks for the reply.<br />Consider this scenario with the buffer being empty and the producer trying to insert an element in buffer.<br />To send a notification the producer thread must take a lock, which will only be released when the consumer releases the lock temporarily while waiting for the buffer to be non-empty.<br />So, when producer gets the lock and adds the element it sends scantyhttps://www.blogger.com/profile/01348208818412787018noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11526903569543714762015-12-20T01:55:50.212-08:002015-12-20T01:55:50.212-08:00@scanty, you are correct in terms of more threads ...@scanty, you are correct in terms of more threads getting notification but that's done purposefully to avoid risk of notification being lost. I would suggest to read <a href="http://javarevisited.blogspot.com/2011/05/wait-notify-and-notifyall-in-java.html" rel="nofollow">difference between notify and notifyAll in Java</a> to learn more. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-3623221040579884442015-12-20T00:47:45.988-08:002015-12-20T00:47:45.988-08:00Thanks for posting a nice explanation of producer ...Thanks for posting a nice explanation of producer consumer problem in java.<br />I have a question regarding use of notifyAll() method here.<br />As we are using two conditional variables for queue full and empty, can't we just use notify() to notify either one consumer or producer(depending upon the conditional variable) ?<br />This will save the overhead of waking up multiple threads, even scantyhttps://www.blogger.com/profile/01348208818412787018noreply@blogger.com