Question

I'm a little stumped. Below is pretty much a copy and paste from A simple scenario using wait() and notify() in java.

To my understanding, this Java program below should be printing yumyum.. to the screen but it isn't. I am in Eclipse for Mac OS X. Any ideas what I'm doing wrong ?

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();
        }
    }
}
Était-ce utile?

La solution 2

Try this...

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();
        }
    }
}

Autres conseils

You have one thread. The single thread will wait indefinitely (it needs to be notified by another thread). Try creating another thread in which one will eatPizza() and one will pizzaGuy

Try below code working fine.

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();
    }

}

}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top