Why wait notify and notifyAll called from synchronized

Tutorials Shared by the Internet Community

Advertisements


Synopsis

Most of Java developer knows that wait() ,notify() and notifyAll() method of object class must have to be called inside synchronized method

Statistics

  • Total Hits - 26839

  • Total Votes - 101 votes

  • Vote Up - 44 votes

  • Vote Down - 57 votes

  • Domain - javarevisited.blogspot.com

  • Category - JAVA/Core JAVA

  • Submitted By - Javin Paul

  • Submitted on - 2011-06-27 15:10:07

Description

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 ...


Site  |   Broken  |   Tweet  |   Facebook  |     |   Save  |   Liked  |   Down

Advertisements