【问题标题】:How to use wait() and notify() in Java?如何在 Java 中使用 wait() 和 notify()?
【发布时间】:2015-03-31 21:12:47
【问题描述】:

据我了解,当我希望当前线程停止工作直到另一个线程在同一个互斥对象上调用 notify() 时,我想在互斥锁上调用 wait()。这似乎不起作用。

我正在尝试制作线程打印 1-10。然后等待另一个线程打印 11-20。然后第一个线程将再次打印 21-30

Main.java

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Object mutex = 1;

        Thread child1 = new Thread(new Child1(mutex));
        Thread child2 = new Thread(new Child2(mutex));

        child1.start();
        child2.start();

    }

}

Child1.java

public class Child1 implements Runnable {
    Object mutex;

    public Child1(Object mutex){
        this.mutex = mutex;
    }

    public void run() {
        synchronized (mutex) {
            for(int c = 0; c < 10; c++){
                System.out.println(c+1);
            }

            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        for(int c = 20; c < 31; c++){
            System.out.println(c+1);
        }
    }
}

Child2.java

public class Child2 implements Runnable {
    Object mutex;

    public Child2(Object mutex) {
        this.mutex = mutex;
    }

    public void run() {
        synchronized (mutex) {
            for (int c = 11; c < 21; c++) {
                System.out.println(c);
            }
            notify();
        }

    }
}

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Exception in thread "Thread-0" 
18
19
20
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:502)
    at task42.Child1.run(Child1.java:18)
    at java.lang.Thread.run(Thread.java:745)
java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at task42.Child2.run(Child2.java:15)
    at java.lang.Thread.run(Thread.java:745)

我错过了什么?

【问题讨论】:

  • @HovercraftFullOfEels,已添加输出。
  • 你在叫什么等待和通知?不在互斥体上!您的代码在调用方法的当前对象上调用这些方法,即 Child1 和 Child2 的 this,而不是互斥体。
  • 你是 wait() Child1 线程和 notify() Child2 线程。你必须在同一个线程上调用 wait() 和 notify()。
  • @BKBatchelor:您不会在线程上调用等待和通知(您可以,因为线程是一个对象-但是您不想),而是在一个物体上。
  • @HovercraftFullOfEels,谢谢!调用互斥体就可以了。

标签: java multithreading mutex


【解决方案1】:

您必须将mutex 引用添加到wait() 和notify();即把wait()改成mutex.wait(),把notify()改成mutex.notify()。

没有这个,你在this调用等待/通知(method()相当于this.method())

这是您的代码,并进行了适当的更改:

Child1.java

public class Child1 implements Runnable {
    Object mutex;

    public Child1(Object mutex){
        this.mutex = mutex;
    }

    public void run() {
        synchronized (mutex) {
            for(int c = 0; c < 10; c++){
                System.out.println(c+1);
            }

            try {
                mutex.wait(); // Changed here
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


        for(int c = 20; c < 31; c++){
            System.out.println(c+1);
        }
    }
}

Child2.java

public class Child2 implements Runnable {
    Object mutex;

    public Child2(Object mutex) {
        this.mutex = mutex;
    }

    public void run() {
        synchronized (mutex) {
            for (int c = 11; c < 21; c++) {
                System.out.println(c);
            }
            mutex.notify(); // Changed here
        }

    }
}

【讨论】:

  • 使用wait 的唯一合理方法是在“条件循环”中循环,直到某个条件变为true(或false)。线程可能会偶然唤醒 - 即使没有 notify。
【解决方案2】:

您的代码以多种方式失败。

首先,不能保证第一个线程也会先运行。 (特别是在多核上,两者并行运行的可能性很高)。所以如果第二个线程先进入Child2.run()的synchronized块,它会在第一个线程处于等待状态之前调用mutex.notify()。结果,第一个线程将永远留在mutex.wait()。

其次,wait() / notify() 不被认为直接用作线程握手机制。这只有在您可以保证第一个线程调用wait()在第二个线程调用notify() 之前才有效。通常,你不能。

相反,wait() 应该用于等待某个条件变为真。条件通常由另一个线程更改,该线程通过调用notifyAll() 通知等待线程。所以握手机制是条件,不是wait/notify:

// 1st thread:
synchronized (lock) {
    while (!condition) {
        lock.wait();
    }
    // continue
}

// 2nd thread:
synchronized {
    condition = true;
    lock.notifyAll();
}

wait() / notify() 或 notifyAll() 的任何其他使用模式都是错误的! 始终在循环内调用 wait() 也非常重要,因为线程可能会偶然唤醒 - 即使没有 notify() 或 notifyAll()。

使用wait()/notifyAll()

因此,在您的情况下,您可以将 wait() 和 notifyAll() 与阶段变量结合使用:

public class Mutex {
    static final Object lock = new Object();
    static int stage = 1;

    static void first() throws InterruptedException {
        synchronized (lock) {
            // we're already in stage 1
            for(int i = 0; i < 10; ++i) System.out.println(i);

            // enter stage 2
            stage = 2;
            lock.notifyAll();

            // wait for stage 3
            while (stage != 3) { lock.wait(); }

            // now we're in stage 3
            for(int i = 20; i < 30; ++i) System.out.println(i);
        }
    }

    static void second() throws InterruptedException {
        synchronized (lock) {
            // wait for stage 2
            while (stage != 2) { lock.wait(); }

            // now we're in stage 2
            for(int i = 20; i < 30; ++i) System.out.println(i);

            // enter stage 3
            stage = 3;
            lock.notifyAll();
        }
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    Mutex.first();
                } catch (InterruptedException ex) { }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                try {
                    Mutex.second();
                } catch (InterruptedException ex) { }
            }
        }).start();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    相关资源
    最近更新 更多