multithreading - Java: notify() vs. notifyAll() all over again -
if 1 googles "difference between notify()
, notifyall()
" lot of explanations pop (leaving apart javadoc paragraphs). boils down number of waiting threads being waken up: 1 in notify()
, in notifyall()
.
however (if understand difference between these methods right), 1 thread selected further monitor acquisition; in first case 1 selected vm, in second case 1 selected system thread scheduler. exact selection procedures both of them (in general case) not known programmer.
what's useful difference between notify() , notifyall() then? missing something?
simply put, depends on why threads waiting notified. want tell 1 of waiting threads happened, or want tell of them @ same time?
in cases, waiting threads can take useful action once wait finishes. example set of threads waiting task finish; once task has finished, waiting threads can continue business. in such case use notifyall() wake waiting threads @ same time.
another case, example mutually exclusive locking, 1 of waiting threads can useful after being notified (in case acquire lock). in such case, rather use notify(). implemented, could use notifyall() in situation well, unnecessarily wake threads can't anyway.
Comments
Post a Comment