In Java, What is the difference between notify and notifyAll in Thread?
Answered
In Java, What is the difference between notify and notifyAll in Thread?
Best answer
Use notify() if all your waiting threads are interchangeable (the order they wake up doesn’t matter), or if you only ever have one waiting thread. A common example is a thread pool used to execute jobs from a queue–when a job is added, one of threads is notified to wake up, execute the next job and go back to sleep.
Use notifyAll() for other cases where the waiting threads may have different purposes and should be able to run concurrently. An example is a maintenance operation on a shared resource, where multiple threads are waiting for the operation to complete before accessing the resource.