Sobes.tech
Junior — Middle

If two threads are in a waiting state and call wake, which one will be awakened first?

sobes.tech AI

Answer from AI

In Android, if two threads are in a waiting state (wait) on the same object and notify (or notifyAll) is called, then:

  • When notify() is called, one arbitrarily chosen thread among those waiting on that object will be awakened. Java does not guarantee the order in which threads are awakened — it depends on the JVM implementation and thread scheduler.

  • When notifyAll() is called, all threads waiting on the object will be awakened, but they will compete for the monitor, and only one can continue execution immediately.

Thus, when wake (notify) is called from multiple waiting threads, one random thread will be awakened, with no guarantee of order.

If two threads are in a waiting state and call wake… - sobes.tech