Java方法NotifyAll()不起作用?

时间:2014-03-29 14:41:57

标签: java eclipse multithreading wait

我有点难过。以下几乎是A simple scenario using wait() and notify() in java的复制和粘贴。

据我了解,下面这个Java程序应该将yumyum..打印到屏幕上,但它不是。我在Eclipse for Mac OS X.我有什么想法我做错了吗?

public class Main {
    public static void main(String[] args) {
        MyHouse house = new MyHouse();
        house.eatPizza();
        house.pizzaGuy();

    }
}

class MyHouse extends Thread {

    private boolean pizzaArrived = false;

    public void eatPizza() {
        synchronized (this) {
            while (!pizzaArrived) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
        System.out.println("yumyum..");
    }

    public void pizzaGuy() {
        synchronized (this) {
            this.pizzaArrived = true;
            notifyAll();
        }
    }
}

3 个答案:

答案 0 :(得分:3)

你有一个主题。单个线程将无限期wait(需要另一个线程通知)。尝试创建另一个帖子,其中一个将eatPizza(),一个将pizzaGuy

答案 1 :(得分:2)

试试这个......

public class Main {

    public static void main(String[] args) {
        MyHouse house = new MyHouse();
        house.start();
//        house.eatPizza();
        // Halt main thread momentarily to delay Mr Pizza Guy
        try { Thread.sleep(3000); } catch(Exception e) {}
        house.pizzaGuy();

    }
}

class MyHouse extends Thread {

    private boolean pizzaArrived = false;
    private Object lock = new Object();

    @Override
    public void run() {
        eatPizza();
    }

    public void eatPizza() {
        synchronized (lock) {
            while (!pizzaArrived) {
                try {
                    System.out.println("Waiting for Pizza guy");
                    lock.wait();
                } catch (InterruptedException e) {
                }
            }
            System.out.println("Pizza arrived!!!");
        }
        System.out.println("yumyum..");
    }

    public void pizzaGuy() {
        synchronized (lock) {
            this.pizzaArrived = true;
            lock.notifyAll();
        }
    }
}

答案 2 :(得分:0)

尝试以下代码正常工作。

public class WaitNotify {

private static int i = 1;
private static boolean flag = false;
static Object obj = new Object();

public static void main(String[] args) {

    Thread t1 = new Thread(new Runnable(){

        @Override
        public void run() {
                while(i<10){
                    synchronized (obj) {
                        try {
                            if(i%2 == 0){
                                obj.wait();
                            }
                            System.out.println("t1 -> " + i++);
                            obj.notify();

                            Thread.currentThread().sleep(500);

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

    Thread t2 = new Thread(new Runnable(){

        @Override
        public void run() {
            while(i<10){
                synchronized (obj) {
                    try {
                        if(i%2 != 0){
                            obj.wait();
                        }
                        System.out.println("t2 -> " + i++);
                        obj.notify();
                        Thread.currentThread().sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }                       
            }   
        }           
    });

    t1.start();
    t2.start();

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

}

}