首页 > 解决方案 > 如何使 notify() 与 wait() 一起正常工作

问题描述

我正在尝试制作一个程序来模拟一个非常简单的洗碗机,它有三个线程。第一个线程负责加水,第二个线程打开设备的门,应该强制加水线程等到第三个线程 notify()。系统运行,但它永远不会停止,并且 notify() 永远不会工作。

import java.util.logging.Level;
import java.util.logging.Logger;

public class threadexample {
    public static boolean flag = false;

    void Open() throws InterruptedException {
        synchronized (threadexample.this) {
            flag = true;
            Thread.sleep(2000);
            System.out.println("producer thread paused");
            wait();
            System.out.println("Resumed");
        }
    }

    void Close() throws InterruptedException {
        synchronized (threadexample.this) {
            flag = false;
            Thread.sleep(6000);
            System.out.println("System resuming..");
            notifyAll();
            Thread.sleep(2000);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        threadexample closing = new threadexample();
        threadexample openning = new threadexample();

        final Door door = new Door();

        // Create a thread object that calls pc.produce()
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                if (flag == false) {
                    for (int check = 0; check <= 8; check++) {
                        if (check == 1) {
                            System.out.println("Adding Water..." + Thread.currentThread().getName());
                        } else {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(threadexample.class.getName()).log(Level.SEVERE, null, ex);
                                if (flag == true) {
                                    try {
                                        closing.Close();
                                    } catch (InterruptedException ex1) {
                                        Logger.getLogger(threadexample.class.getName()).log(Level.SEVERE, null, ex1);
                                    }
                                }
                            }
                        }
                    }
                }

                try {
                    Thread.sleep(4000);

                } catch (InterruptedException ex) {
                    Logger.getLogger(threadexample.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    openning.Open();
                } catch (InterruptedException ex) {
                    Logger.getLogger(threadexample.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    closing.Close();
                } catch (InterruptedException ex) {
                    Logger.getLogger(threadexample.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}

标签: javamultithreadingconcurrencywaitnotify

解决方案


1)你打电话时wait没有检查你等待的事情是否已经发生。如果您wait为已经发生的事情,您将永远等待,因为notify不会再次被调用。

2) 你sleep在拿着锁的时候打电话。那没有意义。

3) 你有:

            threadexample closing = new threadexample();
            threadexample openning = new threadexample();

和:

        synchronized(threadexample.this) 

所以你创建了两个实例,threadexample每个线程在它自己的实例上同步threadexample。那也不对。


推荐阅读