Having hard time to understand the difference in benefits of using notify over notifyall and vice versa. Lets say that we have an object L that we want to lock and four threads: A, B, C and D. Now A enters run method and obtains a lock on object L while B, C and D are waiting to be notified. So what is the difference between notifying these waiting threads individually with notify and notifying all of them at once with notifyall since in both cases we have no control over which thread after A obtains a lock on object L.
Asked
Active
Viewed 51 times
0
-
In both cases one thread will wake if one is waiting, but do you want them all to wake? – Peter Lawrey Jul 04 '18 at 17:55
-
i dont mind if one or all wake and go into a runnable state since it has no effect on the outcome and no effect on which thread will be selected to run next – aziz Jul 04 '18 at 18:15
-
Check this answer https://stackoverflow.com/a/37046/2156877 – Viraj Jul 04 '18 at 18:24
-
the answer made two point the second which is what peter said which really has no effect on the outcome. the second point says use notifyall when u want to select one specific thread to lock the object and wake the rest to do something else. The second point makes no sense because that would be possible if we had the option to decide which specific thread with specific implementation to lock the object. The next thread to lock the object might be from one of the other threads that does something else not related to object resource possession. – aziz Jul 04 '18 at 18:43
-
1yes this question has been posted before but the answers are really vague and paraphrase what peter lawrey has said. – aziz Jul 04 '18 at 18:56