Difference Between notify() and notifyAll()

We can use notify() to give the notification for only one waiting Thread. If multiple Threads are waiting then only one Thread will be notified and the remaining Thread will keep waiting for further information. We can not expect which Thread will be notified if multiple Threads are waiting it depends on JVM.

We can use notifyAll() to give the notification for all waiting Threads of a particular object even though multiple Threads are notified but execution will be performed one by one because Threads require a lock and only one lock is available.

Note- On which object we are calling wait(), Thread required a lock of that particular object for example when calling wait() on object1 then we have to get the lock of object1

Similar Java Tutorials

Leave a Comment