【问题标题】:how to make notify() works properly with wait()如何使 notify() 与 wait() 一起正常工作
【发布时间】:2020-06-09 18:26:35
【问题描述】:

我正在尝试制作一个程序来模拟具有三个线程的非常简单的洗碗机。第一个线程负责加水,第二个线程打开设备的门,应该强制加水线程等到第三个线程 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();
    }
}

【问题讨论】:

  • 您确实应该确保您的代码格式正确。 IDE 甚至可以为您完成这项工作,而且任何人都可以轻松阅读它。

标签: java multithreading concurrency wait notify


【解决方案1】:

您实际上从未调用过openning.Close(),因此从未调用过openning.notify()。因此,线程t1 永远等待。请注意,wait()notify() 调用对象的监视器并且不是静态的。你想使用threadexample lock = new threadExample()

【讨论】:

    【解决方案2】:

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

    2) 你在持有锁的时候调用sleep。这没有意义。

    3) 你有:

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

    和:

            synchronized(threadexample.this) 
    

    因此,您创建了两个threadexample 实例,每个线程在其自己的threadexample 实例上同步。这也不对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2021-03-14
      • 2016-03-21
      • 1970-01-01
      相关资源
      最近更新 更多