1

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();
        }
    }
}
Community
  • 1
  • 1
Martyn Chamberlin
  • 1,277
  • 14
  • 17

3 Answers3

3

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

John Vint
  • 39,695
  • 7
  • 78
  • 108
  • Alas, still not working. Here is my updated main:
    public class Main {
     public synchronized static void main(String[] args) {
      MyHouse house = new MyHouse();
      house.eatPizza();
      MyHouse house1 = new MyHouse();
      house1.pizzaGuy();
     }
    }
    
    – Martyn Chamberlin Mar 29 '14 at 14:47
  • @MartynChamberlin that's even worse. Now you still have only one thread, and call synchronized methods of 2 different objects. To have a thread, you need to call `start()` on a Thread instance somewhere. Otherwise, everything is executed by the main thread. – JB Nizet Mar 29 '14 at 14:51
  • @JBNizet Would you mind posting an example of what this would look like in my main method? I'm invoking .start() (forgot about that, thanks) and it's still not working. Sorry for my thick head. – Martyn Chamberlin Mar 29 '14 at 14:55
  • anonymous just did it. – JB Nizet Mar 29 '14 at 14:56
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();
        }
    }
}
anonymous
  • 1,317
  • 8
  • 7
0

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

}

}

Akash5288
  • 1,919
  • 22
  • 16