wait | notify | notifyAll |Nucleous Software Java Interview Questions: Qu6

wait() | notify() | notifyAll()

Multithreading replaces event loop programming by dividing your tasks into discrete and logical units. Threads also provide a secondary benefit: they do away with polling. Polling is usually implemented by a loop that is used to check some condition repeatedly. Once the condition is true, appropriate action is taken. This wastes CPU time. For example, consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data. In a polling system, the consumer would waste many CPU cycles while it
waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interrocess communication mechanism via the wait( ), notify( ), and notifyAll( ) methods. These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized method. Although conceptually advanced from a computer science perspective, the rules for using these methods are actually quite simple:
  • wait( ) tells the calling thread to give up the monitor and go to sleep until some other
    thread enters the same monitor and calls notify( ).
  • notify( ) wakes up the first thread that called wait( ) on the same object.
  • notifyAll( ) wakes up all the threads that called wait( ) on the same object. The
    highest priority thread will run first.
These methods are declared within Object, as shown here:
final void wait( ) throws InterruptedException
final void notify( )
final void notifyAll( )
--------------------------------------------------------------------------------------
Here you can see that these method are final which means they can not be overiride....

Comments

  1. while using wait() and notify() method in Java Its important to understand that wait() should be called in a loop if multiple thread can wait on same condition because when threads gets notified there is no guarantee which thread will get lock and return from wait method. so if suppose two threads A and B are waiting on a condition and receives notification then both try to get lock , luckily A got lock and executed and made the condition false again but if thread B doesn't check the condition again after getting lock (which is why we need to check condition in loop) it will run even if it supposed to wait. So its important to recheck the condition after getting lock and wait again if condition is false again.

    JP
    Why wait() and notify() method must be called from synchronized context

    ReplyDelete

Post a Comment

Popular posts from this blog

Struts 2 - struts tag with boolean property

Android: where to run keytool command in android