Before you look at these two methods, take a look at the concepts of lock pool (EntryList) and wait pool (WaitSet)

Lock pool

Let’s say thread A already owns the lock on an object (not A class), and other threads B and C want to call A sychronized method (or block) on the object. Since thread B and C must acquire ownership of the lock on the object before they can access the sychronized method (or block) on the object, the lock is being locked by thread A This point is the lock pool of the object. Briefly, this is the place where the thread that needs to acquire the lock waits.

Waiting for the pool

If thread A calls an object’s wait method, thread A releases the object’s lock, and thread A enters the object’s wait pool without competing for the object’s lock

Important: Lock pools and wait pools are object specific

The difference between notify and notifyAll

  • NotifyAll causes all threads in the wait pool to enter the lock pool to compete for the lock
  • Notify will only randomly select a thread from the waiting pool to enter the lock pool to compete for the lock