为什么我的release()方法没有唤醒我的实例?

时间:2013-04-20 05:09:59

标签: java multithreading concurrency semaphore notify

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

我的问题是Bartender类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);
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

class Bartenderclass Customer

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

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

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

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