concurrency - Java - notify() vs notifyAll() - possible deadlock? -


Is there any condition in which notify () can cause a deadlock, but Notify all () - Never?

For example, in the case of multiple locks, notify () to indicate only one thread, which checks for any object lock and again Waiting, though another thread can unlock that object. In the case of using notifyAll () , all threads will be notified to run and none of them will surely unlock that item for it.

Yes imagine that you apply to the manufacturer - consumer problem synchronize, wait, And notify with . (edit) where 2 producers and 2 users all wait on the same object monitor (end edit) . Corresponding to the manufacturer calls this implementation. Now suppose that you have two threads running the manufacturer's code path. It is possible that the Creator 1 call notify and the Creator 2 wakes up to know that the Creator 2 can not do any work and later to call notify Fails you are now deadlock.

(edit) If notified all has been called, then both consumer 1 and consumer 2 have jumped in addition to the manufacturer 2. One of the consumers consumed the data to wake at least one manufacturer and in turn was called notifyAll , thus it successfully allowed a very broken implementation with a lame. .

The reference question I close my scenario:


Comments