java - How to get same monitor when calling notifyAll()? -



java - How to get same monitor when calling notifyAll()? -

main thread creating 2 thread t1 , t2 run() method of these thread creating 2 new thread c1 , c2.i want scenario such until c1&c2(of t1) live t2 not start executing. in code notify , wait causing runtime exception.since not in synchronised block, how this?

public class childtcreat2newthread { public static void main(string[] args) throws interruptedexception { thread maint=thread.currentthread(); target ra=new target("a"); thread t1=new thread(ra); t1.start(); t1.join(); while(ra.getc1().isalive()==true||ra.getc2().isalive()==true){ synchronized (maint) { maint.wait(); }} new thread(new target("b")).start();}} class target implements runnable{ thread c1=new thread(new target1("1")); thread c2=new thread(new target1("2")); string msg; target(string msg){ this.msg=msg; } @override public void run() { for(int j=0;j<100000;j++){ for(int i=0;i<10000;i++){ if(i%10000==0&&j%10000==0){system.out.print(msg);} }} t1.start(); t2.start(); } public thread getc1(){return c1;} public thread getc2(){return c2;} } class target1 implements runnable { string msg; target1(string msg){ this.msg=msg; } @override public synchronized void run() { for(int j=0;j<100000;j++){ for(int i=0;i<100000;i++){ if(i%100000==0&&j%10000==0){system.out.print(msg);} } } try{ notifyall(); system.out.println("k");}catch(illegalmonitorstateexception e){system.out.println("\nillegalmonitorstateexception!! in "+msg+"\n");} } }

wait( ) tells calling thread give monitor , go sleep until others thread enters same monitor , calls notify( ).unable same monitor when calling notify.how this?

as understanding both thread t1 & t2 not have mutual object here these accessing object should have pass in synchronised lock phone call wait() , notify()?

as @jb nizet pointed out should utilize bring together wait fot thread termination

edit since cannot utilize bring together suggest utilize countdownlatch since documentation states:

a synchronization aid allows 1 or more threads wait until set of operations beingness performed in other threads completes.

which asked for.

second edit

here modified version of code wait thread termination using homemade countdownlatch uses wait , notify.

import java.util.concurrent.countdownlatch; public class childtcreat2newthread { public static void main(string[] args) throws interruptedexception { mycountdownlatch donesignal = new mycountdownlatch(2); target ra = new target("a",donesignal); thread t1 = new thread(ra); t1.start(); donesignal.await(); system.out.println("after await "); mycountdownlatch donesignal1 = new mycountdownlatch(2); new thread(new target("b",donesignal1)).start(); } } class target implements runnable { private thread c1; private thread c2; string msg; target(string msg, mycountdownlatch donesignal) { this.msg = msg; c1 = new thread(new target1("1",donesignal)); c2 = new thread(new target1("2",donesignal)); } @override public void run() { system.out.println("start of target " + msg); (int j = 0; j < 100000; j++) { (int = 0; < 10000; i++) { if (i % 10000 == 0 && j % 10000 == 0) { system.out.print(msg); } } } c1.start(); c2.start(); // seek { // c1.join(); // c2.join(); // } grab (interruptedexception e) { // // todo auto-generated grab block // e.printstacktrace(); // } system.out.println("end of target " + msg); } public thread getc1() { homecoming c1; } public thread getc2() { homecoming c2; } } class target1 implements runnable { string msg; private mycountdownlatch donesignal; target1(string msg, mycountdownlatch donesignal) { this.msg = msg; this.donesignal=donesignal; } @override public void run() { system.out.println("start of target1 " + msg); (int j = 0; j < 100000; j++) { (int = 0; < 100000; i++) { if (i % 100000 == 0 && j % 10000 == 0) { system.out.print(msg); } } } seek { system.out.println("k"); donesignal.countdown(); system.out.println("end of target1 " + msg); } grab (illegalmonitorstateexception e) { system.out.println("\nillegalmonitorstateexception!! in " + msg + "\n"); } } } class mycountdownlatch { private int waitersnum; public mycountdownlatch(int waitersnum) { this.waitersnum=waitersnum; } public synchronized void countdown() { waitersnum--; if (waitersnum==0) { notifyall(); } } public synchronized void await() throws interruptedexception { wait(); } }

java multithreading

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -