java - Difference between notify() and notifyAll() -
i know similar questions have been discussed in site, have not still got farther aid considering specific example. can grasp difference of notify() , notifyall()
regarding thread
"awakeining" in theory cannot perceive how influence functionality of programme when either of them used instead of other. hence set next code , know impact of using each 1 of them. can start give same output (sum printed 3 times).
how differ virtually? how modify program, in order applying notify or notifyall
play crucial role functionality (to give different results)?
task:
class mywidget implements runnable { private list<integer> list; private int sum; public mywidget(list<integer> l) { list = l; } public synchronized int getsum() { homecoming sum; } @override public void run() { synchronized (this) { int total = 0; (integer : list) total += i; sum = total; notifyall(); } }
}
thread:
public class myclient extends thread { mywidget mw; public myclient(mywidget wid) { mw = wid; } public void run() { synchronized (mw) { while (mw.getsum() == 0) { seek { mw.wait(); } grab (interruptedexception e) { e.printstacktrace(); } } system.out.println("sum calculated thread " + thread.currentthread().getid() + " : " + mw.getsum()); } } public static void main(string[] args) { integer[] array = { 4, 6, 3, 8, 6 }; list<integer> integers = arrays.aslist(array); mywidget wid = new mywidget(integers); thread widthread = new thread(wid); thread t1 = new myclient(wid); thread t2 = new myclient(wid); thread t3 = new myclient(wid); widthread.start(); t1.start(); t2.start(); t3.start(); }
}
update: write explicitly. result same whether 1 uses notify or notifyall: sum calculated thread 12 : 27 sum calculated thread 11 : 27 sum calculated thread 10 : 27
therefore question: difference?
the difference subtler illustration aims provoke. in words of josh bloch (effective java 2nd ed, item 69):
... there may cause utilize notifyall
in place of notify
. placing wait
invocation in loop protects against accidental or malicious notifications on publicly accessible object, using notifyall
in place of notify
protects against accidental or malicious wait
s unrelated thread. such wait
s otherwise “swallow” critical notification, leaving intended recipient waiting indefinitely.
so thought must consider other pieces of code entering wait
on same monitor waiting on, , other threads swallowing notification without reacting in designed way.
other pitfalls apply well, can result in thread starvation, such several threads may wait different conditions, notify
happens wake same thread, , 1 status not satisfied.
even though not related question, sense of import quote conclusion (emphasis original author):
in summary, using wait
, notify
straight programming in “concurrency assembly language,” compared higher-level language provided java.util.concurrent
. there seldom, if ever, reason utilize wait
, notify
in new code. if maintain code uses wait
, notify
, create sure invokes wait
within while
loop using standard idiom. notifyall
method should used in preference notify
. if notify
used, great care must taken ensure liveness.
java multithreading notify
No comments:
Post a Comment