0

我编写了一个示例程序来演示我的问题。有一个调酒师线程和三个客户线程。它们在创建后同时运行。调酒师应该为每位顾客提供一杯饮料。

我的问题是调酒师类 run() 方法中的 wait() 方法永远不会唤醒。我原本打算让每个 Customer 类的 run() 方法中的 release() 方法唤醒它,但它似乎不起作用。它永远不会醒来。

我该如何解决这个问题?感谢任何可以提供建议或代码片段的人。

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

public class Bar {

    Semaphore serving;
    boolean isServing = false;

    public static void main(String[] args) {
        Bar bar = new Bar();
        bar.run();
    }

    public void run() {
        serving = new Semaphore(1);
        Thread bartender = new Thread(new Bartender());
        bartender.start();
        threadSleep(1000);

        Thread customer1 = new Thread(new Customer());
        customer1.start();
        threadSleep(2000);
        Thread customer2 = new Thread(new Customer());
        customer2.start();
        threadSleep(2000);
        Thread customer3 = new Thread(new Customer());
        customer3.start();
    }

    public void threadSleep(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException ex) {
            Logger.getLogger(Bar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public class Bartender implements Runnable {
        public void run() {
            while (true) {
                if (serving.availablePermits() == 0) {
                    synchronized (this) {
                        try {
                            System.out.println("Waiting for Customer notify");
                            wait();
                            System.out.println("Serve drink");
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Bar.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        }
    }

    public class Customer implements Runnable {
        private boolean customerServed = false;

        public void run() {
            if (!customerServed) {
                synchronized (this) {
                    try {
                        serving.acquire();
                        if (serving.availablePermits() == 0 && !serving.hasQueuedThreads()) {
                            notify();
                            isServing = true;
                            System.out.println("Customer: Recieves drink");
                            customerServed = true;
                            serving.release();
                            isServing = false;
                        }
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Bar.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }
}
4

1 回答 1

2

class Bartenderclass Customer

更改synchronized (this) {synchronized (Bar.this) {

更改wait()Bar.this.wait()

更改notify()Bar.this.notify()

因为两个this引用不同的对象,所以Bartender永远不会醒来。并且因为两个Bar.this引用同一个对象,Bartender会被唤醒!

于 2013-04-20T05:17:13.460 回答