Assume there are 2 or more threads waiting on that lock. Is it guaranteed that two threads are woken up?
Yes. Each notify takes a thread from the waiting queue and puts it in the blocked queue -- the awoken thread must first get access to the synchronized
lock in question. If there is only 1 thread waiting on the lock then the 2nd notify()
would do nothing.
It is important to realize that the thread will not start executing immediately. Since it had to be in a synchronized
block on lock
to be able to do the wait()
it must get access to the lock
again before it can run. There may be multiple other threads already in the block queue, waiting to get access to lock
.