1

考虑以下代码:-

public class UsingWait1{
public static void main(String... aaa){
    CalculateSeries r = new CalculateSeries();
    Thread t = new Thread(r);
    t.start();
    synchronized(r){
        try{
            r.wait();   //Here I am waiting on an object which is Runnable. So from its run method, it can notify me (from inside a synchronized block).
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }

    }
    System.out.println(r.total);
    try{
        Thread.sleep(1);
    } catch (InterruptedException e){
        System.out.println("Interrupted");
    }
    System.out.println(r.total);

}
}

class CalculateSeries implements Runnable{
int total;
public void run(){
synchronized(this){
    for(int i = 1; i <= 10000; i++){
        total += i;
    }
    notify();   // Line 1 .. Notify Exactly one of all the threads waiting on this instance of the class to wake up
}
}
}

在这里,我正在等待可运行的 CalculateSeries。所以我可以从CalculateSeries的run()方法通知等待线程。

但是现在,考虑下面的代码,我正在等待一个不可运行的对象。

public class WaitNotOnThread{
public static void main(String... aaa){
    NotRunnable nr = new NotRunnable();
    IAmRunnable r = new IAmRunnable(nr);
    new Thread(r).start();
    synchronized(nr){
        try{
            nr.wait();
        } catch(InterruptedException e){
            System.out.println("Wait interrupted");             
        }

        System.out.println("After being notified within synchronized");
    }
    System.out.println("After synchronized");
}
}

class IAmRunnable implements Runnable{
NotRunnable nr;
IAmRunnable(NotRunnable nr){
    this.nr = nr;
}
public void run(){
    synchronized(nr){
        try{
            Thread.sleep(1000);
        } catch(InterruptedException e){
            System.out.println("Sleeping Interrupted :( ");
        }
        notify();                  // Line 2
    }
}
}

class NotRunnable{
}

在这里,我在第 2 行得到一个 IllegalMonitorStateException。我在调用 wait() 和 notify() 时,正在等待对象的同一个实例(不是 Runnable)。那么问题是什么?

有人还可以给出一些场景,在这些场景中等待一个不可运行的对象会很有用吗?

4

1 回答 1

4

等待不需要在 Runnable 上。这就是 notify() 在Object 而不是Runnable上的原因。我想这在我们希望避免忙碌等待的所有情况下都有帮助。

问题似乎是 synchronized() 在 nr 上,并且在不同的对象上调用了通知。同步也应该在最终变量上。

class IAmRunnable implements Runnable {
     final NotRunnable nr;

    IAmRunnable( final NotRunnable nr) {
        this.nr = nr;
    }

    public void run() {
        synchronized (nr) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("Sleeping Interrupted :( ");
            }
            nr.notify();                  // Line 2
        }
    }
}
于 2012-01-25T06:00:32.963 回答