educative.io

Why did we use notifyAll() instead of notify()

Why did we use notifyAll() instead of notify() in in line 36

synchronized (this) {            
empsInBathroom--;
if (empsInBathroom == 0) inUseBy = NONE;
// Since we might have just updateded the value of
// inUseBy, we should notifyAll waiting threads
this.notifyAll();        
}

@HHH we are calling notifyAll() because we want all of our waiting threads to try to access the critical section giving every thread the same opportunity but if we had used notify() then this method would have notified only one thread in the waiting queue.