Interview Questions

Difference between notify() and notifyAll() methods, can you write a code to prove your point?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

34. Difference between notify() and notifyAll() methods, can you write a code to prove your point?

Theoretically you must have heard or you must be aware of differences between notify() and notifyAll().But have you created program to achieve it? If not let’s do it.

First, I will like give you a brief description of what notify() and notifyAll() methods do.

notify() - Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is random and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object.

public final native void notify();

notifyAll() - Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object.

(Continued on next question...)

Other Interview Questions