In a Producer & Consumer thread problem, producer thread has acquired the lock and multiple consumer threads are in wait pool, which consumer thread gets notified first, once producer thread calls notify?

Answer:

The thread that it notifies depends on JVM implementation. We can’t really say which thread actually gets notified. It could be the first thread, it could be the last thread which went into the wait pool or it could be the highest priority thread.

Most often the JVM implementation is as first thread goes into the wait pool is the first one to be notified because if assume it notifies the last thread went into the pool, first thread might end up waiting for long period the same case may happen with the highest priority thread also. So, therefore general implementation would be the first thread goes into the wait pool gets notified first.

In general answer to this question is not sure, its depends upon JVM implementation.

Note – in case of notifyAll() all the treads in wait pool get notified and the highest priority thread will acquire the lock first.

Related Posts