tag:blogger.com,1999:blog-8712770457197348465.post4922332328260904194..comments2024-03-29T05:54:46.190-07:00Comments on Javarevisited: Why wait, notify, and notifyAll methods are called from synchronized block or method in Java?javin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger31125tag:blogger.com,1999:blog-8712770457197348465.post-27437785797757396142018-10-07T11:27:46.806-07:002018-10-07T11:27:46.806-07:00Hello Javin,
What if the variable used to set &...Hello Javin,<br />What if the variable used to set & check condition is declared volatile? If volatile, could we have got away from using the synchronized blocks?<br />Thanks.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87414164159003788692016-10-19T19:25:26.185-07:002016-10-19T19:25:26.185-07:00Bad explanation.
Step 1 says "The Producer ...Bad explanation. <br /><br />Step 1 says "The Producer thread tests the condition (buffer is full or not) and confirms that it must wait (after finding buffer is full)"<br /><br />And then Step 3 says "3. The Consumer thread calls the notify () method; this goes unheard since the Producer thread is not yet waiting."<br /><br />Total contradiction. Step 1 says Producer must M Chistyhttps://www.blogger.com/profile/12628189108377300462noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86147694629165982342016-01-29T00:17:41.123-08:002016-01-29T00:17:41.123-08:00As we know wait()/notify() methods are used for in...As we know wait()/notify() methods are used for inter thread communication. In java Inter thread communication means acquiring or releasing the lock on an object. The lock on any object is enabled only when you use synchronized method/block.<br /><br />If you call wait() in non-synchronised block/method, it dont had any lock on any object. So no question of releasing the lock at all. This is the Vishwanatrh G Tnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55649166915888614282015-08-27T02:11:25.154-07:002015-08-27T02:11:25.154-07:00Hi Anonymous, the problem in your code is on which...Hi Anonymous, the problem in your code is on which you are calling the notify. I see that you are having a static Thread on which you are calling the notify, but the wait is happening on the main thread. I modified your code and changed it to notify() instead of b.notify() and it worked<br />VikasAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27620268121915205452015-06-25T00:32:50.691-07:002015-06-25T00:32:50.691-07:00package qwerty;
class customer12 extends Thread
{...<br />package qwerty;<br /><br />class customer12 extends Thread<br />{<br /> int amount=10000;<br /> //synchronized <br /> static Thread b;<br /> void withdraw(int amount)<br /> {<br /> System.out.println("Going to withdraw");<br /> if(this.amount<amount)<br /> {<br /> b=Thread.currentThread();<br /> System.out.println(&Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12150577729703365082015-05-18T13:51:02.288-07:002015-05-18T13:51:02.288-07:00What will happen if multiple wait() will call noti...What will happen if multiple wait() will call notify() method and not notifyAll() ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17192076924530808072015-04-10T11:52:19.978-07:002015-04-10T11:52:19.978-07:00Hi,
When thread calls wait on an object, it relea...Hi,<br /><br />When thread calls wait on an object, it releases the lock it holds on that object and goes to wait, this is the reason we need to call wait inside synchronized block. when thread calls notify on that object, waiting thread come out of the object gets lock and continues execution. why we need to call notify inside synchronized block..? why thread needs lock on the object to wake upManjunath.SVhttps://www.blogger.com/profile/16406409023754670121noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48832346529747889162014-05-17T08:12:04.519-07:002014-05-17T08:12:04.519-07:00Friends,
Simple answer is to let Java know which o...Friends,<br />Simple answer is to let Java know which object lock should be released when wait method is called, as thread may have multiple lock. Calling wait outside sync context well leave Java clueless on which lock should release.<br /><br />Thanks,<br />Gk Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-81517769396627295672014-04-28T21:14:23.464-07:002014-04-28T21:14:23.464-07:00Race condition can happen even with wait()/notify(...Race condition can happen even with wait()/notify()/notifyAll() being called from synchronized context. Race condition is prevented by conditional check on shared state of the object in loop around call of wait().<br /><br />Example:<br />public void run() {<br /> while (true) {<br /> synchronized (jobs) {<br /> // wait until at least one job is available<br /> whileYaduhttps://www.blogger.com/profile/18388446465037858805noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-2407381909524643052014-03-29T22:08:20.747-07:002014-03-29T22:08:20.747-07:00I am not sure if your producer-consumer example is...I am not sure if your producer-consumer example is correct from the standpoint of race condition. What I mean is, producer uses push(Object x) function to put something in queue -- so this will be synchronized for all producer threads. Similarly a consumer will use the pop() method to empty the queue -- so this will be synchronized only for all consumer threads. If either of these methods are notbhsnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67880865692968117452014-01-28T04:52:19.703-08:002014-01-28T04:52:19.703-08:00Javin your argument is incorrect. Lets assume that...Javin your argument is incorrect. Lets assume that inside Object class wait() and notify() method where synchronized. Now your producer/consumer solution need not to call these method from synchronized block. Will producer/consume work correctly? No because 'is buffer empty condition check and wait' must be atomic. Similarly, 'buffer is not empty check and notify' must be atomic Nitin Taurhttps://www.blogger.com/profile/13907059707633519918noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-49452976644572697212013-12-24T07:20:29.566-08:002013-12-24T07:20:29.566-08:00Wont the compiler throw error when we try to call ...Wont the compiler throw error when we try to call wait/notify from non-synchronized method?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85765460599752618392013-10-01T23:16:02.757-07:002013-10-01T23:16:02.757-07:00I super like your bloggings. Whenever i feel doubt...I super like your bloggings. Whenever i feel doubt regarding any topic i always go for your articles.<br /><br />Here in this article i have one doubt that when "The Consumer thread calls the notify () method; this goes unheard since the Producer thread is not yet waiting." why Producer thread not go in wait state?<br />how come this notification lost?Vandna Thakurhttps://www.blogger.com/profile/08255701552282349827noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43284068889393020702013-06-12T00:52:25.513-07:002013-06-12T00:52:25.513-07:00We can not release a lock(using wait()) or notifyi...We can not release a lock(using wait()) or notifying other threads to acquire lock with owning lock for any object. So before calling this function we need to own a lock using synchronized block or synchronized method.satpanhttps://www.blogger.com/profile/10315731926212186892noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-8452204502288933982013-05-31T00:27:55.630-07:002013-05-31T00:27:55.630-07:00I think this is a simple question,you should only ...I think this is a simple question,you should only answer : wait notify notifyAll is designed by thread communication.Anonymoushttps://www.blogger.com/profile/03672295627923809238noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9294242688060690062013-03-07T03:17:48.457-08:002013-03-07T03:17:48.457-08:00@Achappn M, thanks for comment. Since buffer is fu...@Achappn M, thanks for comment. Since buffer is full, producer can no longer add new items into buffer, hence producer thread should wait, until consumer, create space in buffer after consuming some items.Javin @ producer consumer solution BlockingQueuehttp://javarevisited.blogspot.com/2012/02/producer-consumer-design-pattern-with.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68705465488822275542013-01-28T02:11:16.282-08:002013-01-28T02:11:16.282-08:00I have been following up this blog for a long time...I have been following up this blog for a long time and I am very much impressed with it. Lovely articles, Clear explanation and much more. Thank you for the wonderful blog.Jawaharhttps://www.blogger.com/profile/01088934162867718109noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69474587699454431382013-01-26T19:56:51.665-08:002013-01-26T19:56:51.665-08:00wait method is called to release lock which thread...wait method is called to release lock which thread has acquired and thread can acquire intrinsic lock only within synchronized block. If wait method has called outside synchronized block it does not know which lock should it release if it is not holding any lock. I am really skeptical about race condition being the reason for wait method to call inside synchronized blockAmol Pawarhttps://www.blogger.com/profile/06133827287204837664noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50177593507122289952012-08-02T00:45:46.764-07:002012-08-02T00:45:46.764-07:00Operating system uses a concept of "monitor&q...Operating system uses a concept of "monitor" and "waitset" to handle multiple threads accessing a resource.In java this concept is implemented using "synchronized" and "'wait','notify' and 'notifyAll'".Here monitor refer to 'synchronized' and waitset includes all three methods.(as far as my knowledge is true)nitusnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30011006739920129992012-07-22T23:46:37.915-07:002012-07-22T23:46:37.915-07:00hi boss nice explanation & i hv a question,
c...hi boss nice explanation & i hv a question,<br /><br />can we invoke a specific thread (t2) from object monitor state(assume 5 threads like t1,t2...t5)Anonymoushttps://www.blogger.com/profile/13569553022723020300noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-91260862938077336152012-04-19T03:12:20.028-07:002012-04-19T03:12:20.028-07:00All is fine, i am bit not comfortable with point #...All is fine, i am bit not comfortable with point #3 The Consumer thread calls the notify () method; this goes unheard since the Producer thread is not yet waiting.<br /><br />how come this notification lost.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14818717279157304322012-02-24T07:21:47.814-08:002012-02-24T07:21:47.814-08:00@Anonymous. you got it right. it means "if yo...@Anonymous. you got it right. it means "if you don't use synchronized method or block for calling wait() and notify() method it will throw IllegalMonitorStateException"Javin @ why wait and notify defined in Object classhttp://javarevisited.blogspot.com/2012/02/why-wait-notify-and-notifyall-is.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-41761393656871562302012-02-24T03:18:24.347-08:002012-02-24T03:18:24.347-08:00Just to summarize we call wait (), notify () or no...Just to summarize we call wait (), notify () or notifyAll method in Java from synchronized method or synchronized block in Java to avoid:<br />1) IllegalMonitorStateException in Java which will occur if we don't call wait (), notify () or notifyAll () method from synchronized context.<br /><br />Here are you saying that IllegalMonitorStateException will be thrown if you do NOT call wait/Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-35960874525913803412011-11-29T04:47:47.930-08:002011-11-29T04:47:47.930-08:00Thanks for you comment Anonymous, Good to know tha...Thanks for you comment Anonymous, Good to know that you find information on wait and notify useful.Javin @ thread interview questionshttp://javarevisited.blogspot.com/2011/07/java-multi-threading-interview.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-19643355245368415512011-11-28T09:15:57.233-08:002011-11-28T09:15:57.233-08:00thanks!
clean and precisethanks!<br />clean and preciseAnonymousnoreply@blogger.com