3

I know that this is a repeated question. But I am unable to understand by the explanation. I want to understand it cleraly with a well example. Can any one please help.

"Why we call wait(), notify() methods from a synchronized context".

aj983
  • 293
  • 2
  • 5
  • 12
  • Wait and notify don't mean anything if you're not looking for synchronization. And they're ineffective if you don't use them to synchronize. – ben Nov 20 '11 at 05:23
  • I know that sir. But I am asking what is the reason to call them from synchronized context. – aj983 Nov 20 '11 at 05:25
  • This item is a duplicate and can be answered adequately here: http://stackoverflow.com/questions/2779484/why-must-wait-always-be-in-synchronized-block –  Oct 14 '15 at 01:57

2 Answers2

0

We use wait () and notify () or notifyAll () method mostly for inter-thread communication.

One thread is waiting after checking a condition e.g. In Producer Consumer example Producer Thread is waiting if buffer is full and Consumer thread notify Producer thread after he creates a space in buffer by consuming an element. calling notify() or notifyAll() issues a notification to a single or multiple thread that a condition has changed and once notification thread leaves synchronized block , all the threads which are waiting fight for object lock on which they are waiting and lucky thread returns from wait() method after reacquiring the lock and proceed further. Let’s divide this whole operation in steps to see a possibility of race condition between wait () and notify () method in Java, we will use Produce Consumer thread example to understand the scenario better:

  1. The Producer thread tests the condition (buffer is full or not) and confirms that it must wait (after finding buffer is full).
  2. The Consumer thread sets the condition after consuming an element from buffer.
  3. The Consumer thread calls the notify () method; this goes unheard since the Producer thread is not yet waiting.
  4. The Producer thread calls the wait () method and goes into waiting state.

So due to race condition here we potential lost a notification and if we use buffer or just one element Produce thread will be waiting forever and your program will hang. In conclusion in order to avoid the race condition, wait(), notify() and notifyAll() is called from a synchronized context.

HTH.

benz
  • 4,561
  • 7
  • 37
  • 68
  • Or you could have given this link - http://javarevisited.blogspot.com/2011/05/wait-notify-and-notifyall-in-java.html – Anshu May 28 '15 at 15:44
0

when we use synchronize in threads, it means we do make a lock on that object and that object can be used only by one thread at one time to avoid various kinds of concurrency issues.

wait() and notify() methods are used only from synchronized context.

wait method pauses the working of current thread and releases the lock it is holding on any object so that other threads can use that synchronized object.

when other thread is done with its work then it calls the notify method which causes the first thread to again obtain the lock on the object and resume its working.

you can easily get examples from net.

gprathour
  • 14,813
  • 5
  • 66
  • 90