开发者

Why should I use notify?

I'm reading through my SCJP book and I'm on the chapter about threads, and I am wondering why you should use n开发者_开发知识库otify at all.

Here is my sample program using notify:

class ThreadA {
    public static void main(String[] args){
        ThreadB b = new ThreadB();
        b.start();

        synchronized(b){
            try{
                System.out.println("Waiting for b to complete...");
                b.wait();
            }catch(InterruptedException e) {}
            System.out.println("Total is: " + b.total);
        }
    }
}

class ThreadB extends Thread {
    int total;
    public void run() {
        synchronized(this) {
            for(int i = 0; i < 100000000; i++){
                total++;
            }
            notify();
        }       
        for(int i = 0; i < 100000000; i++){
            total++;
        }
    }
}

If I take out the notify call, it still executes in the same way. I.E., once the lock is released, the b.wait() stops blocking eventually and we get a semi random number between 100000000 and 200000000, depending on the scheduler.

This code:

class ThreadA {
    public static void main(String[] args){
        ThreadB b = new ThreadB();
        b.start();

        synchronized(b){
            try{
                System.out.println("Waiting for b to complete...");
                b.wait();
            }catch(InterruptedException e) {}
            System.out.println("Total is: " + b.total);
        }
    }
}

class ThreadB extends Thread {
    int total;
    public void run() {
        synchronized(this) {
            for(int i = 0; i < 100000000; i++){
                total++;
            }
            notify();
        }       
    }
}

Always results in 100000000 getting printed regardless wither the notify is there or not.

And this code:

class ThreadA {
    public static void main(String[] args){
        ThreadB b = new ThreadB();
        b.start();

        synchronized(b){
            try{
                System.out.println("Waiting for b to complete...");
                b.wait();
            }catch(InterruptedException e) {}
            System.out.println("Total is: " + b.total);
        }
    }
}

class ThreadB extends Thread {
    int total;
    public void run() {
        synchronized(this) {
            for(int i = 0; i < 100000000; i++){
                total++;
            }
            notify();
            for(int i = 0; i < 100000000; i++){
                total++;
            }
        }       
    }
}

Always prints out 200000000 regardless of the notify being present or absent.

So as far as I can tell, the only thing the notify does is possibly waking up a thread earlier than needed, if that is the case, why use notify at all? Why not wait for the lock to be released and let the JVM restart the other thread?


Unless you or someone else calls notify(), the wait() should continue forever, barring spurious wakes. Are you sure nothing's interrupting the thread?

Basically you use notify() precisely to wake up threads. If you don't need to put a thread to sleep until another thread can notify it that it should wake up, don't use it.

EDIT: Okay, I've reproduced this behaviour - and I suspect it's because you're calling wait() on the thread object itself. My guess is that the Thread object gets notified when the thread terminates.

Try waiting on a shared plain Object, like this:

class ThreadA {

    static Object monitor = new Object();

    public static void main(String[] args) {
        ThreadB b = new ThreadB();
        b.start();

        synchronized(monitor) {
            try {
                System.out.println("Waiting for b to complete...");
                monitor.wait();
            }catch(InterruptedException e) {}
            System.out.println("Total is: " + b.total);
        }
    }
}

class ThreadB extends Thread {
    int total;
    public void run() {

        synchronized(ThreadA.monitor) {
            for (int i = 0; i < 100000000; i++) {
                total++;
            }
//            ThreadA.monitor.notify();
        }       
    }
}

If you uncomment that line, the program terminates - otherwise it doesn't.

EDIT: I've actually found some documentation on this. From Thread.join(millis, nanos):

As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜