user4768611
user4768611

Reputation:

how to get same monitor when calling wait() ,notify() or notifyAll()?

main thread creating two thread t1 and t2 run() method of these thread creating two new thread c1 and c2.I want a scenario such that until c1&c2(of t1) are alive t2 will not start executing. In my code notify and wait are causing Runtime Exception.Since they are not in synchronised block, how to do 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 the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).Unable to get same monitor when calling notify.How to do this? and what does its importance?

Upvotes: 0

Views: 114

Answers (3)

vsr
vsr

Reputation: 63

As i don't have enough reputation to comment, commenting via answer

You are not locking and notifying on same object.

you are locking on mainT but notifying on Target1 instance, you need to pass the locking object to c1 and c2.

however i suggest you to use java concurrent API to solve such problems

Upvotes: 1

mkm13
mkm13

Reputation: 141

You should avoid wait()/notify() and wherever possible use the higher-level abstractions Java provides, see: Java Concurrency Tutorial.

Using wait()/notify(), you must use the same monitor for both calls, see Guarded Blocks. Even if this is only for learning, I don't see a good way of making this work with wait(). You should either stick to join() or use one of the higher-level abstractions.

Upvotes: 1

yunandtidus
yunandtidus

Reputation: 4096

What about :

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.getC1().join();
    t1.getC2().join()

    new Thread(new Target("b")).start();
}

See : https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join%28%29

Upvotes: 0

Related Questions