multithreading - Thread not printing everything after notifyAll in java -
multithreading - Thread not printing everything after notifyAll in java -
class lock implements runnable{ int i=0; public synchronized void run(){ for(i=0;i<10;i++){ if(thread.currentthread().getname().equals("t1") && == 5) {try { this.wait();} catch(interruptedexception ie){}} system.out.print(thread.currentthread().getname()+" "); if(thread.currentthread().getname().equals("t3") && == 9) this.notifyall(); } } } public class threadlock { public static void main(string[] args){ lock l = new lock(); thread t1 = new thread(l); thread t2 = new thread(l); thread t3 = new thread(l); t1.setname("t1"); t2.setname("t2"); t3.setname("t3"); t1.start(); t2.start(); t3.start(); } }
output is: t1 t1 t1 t1 t1 t3 t3 t3 t3 t3 t3 t3 t3 t3 t3 t1 t2 t2 t2 t2 t2 t2 t2 t2 t2 t2
t1 not printing 10 times after calling notifyall method. ran many times every time t1 printed 6 times. why t1 not printing 10 times? please reply soon
i
instance variable. it's shared threads. should local variable.
when first thread awakened, reacquires lock, prints name, increments i, , reevaluates if i < 10
. since other threads have set i
10 already, loop stops.
java multithreading wait synchronized notify
Comments
Post a Comment