0

Im trying to understand how implement Dining Savages using monitors. I have three classes, and im using the kitchen class to act as the monitor for when the pot is empty or not.

For some reason i keep getting a null pointer exception at thread two in my example below.

class Kitchen {
  Kitchen k;
  int c;
  boolean empty;
  Cook chef;
  Kitchen() {
    this.c = 0;
    this.empty = true;
    chef = new Cook(k);

  }
  synchronized void putServingsInPot(int servings) {
    if (empty) {
        this.c = servings;
    }
    empty = false;

    notify();
  }
synchronized void getServingsFromPot() {
    while (empty) {
        try {
            System.out.println("Bout to wait");
            wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println("In Catch");
            e.printStackTrace();
        }if (c == 0) {
            empty = true;
            chef.run();
        }else if(c > 0 && empty == false){
            c--;
        }
    }
}

}

class Savage extends Thread {
  Kitchen k;
  Savage(Kitchen k) {
    this.k = k;
  }
  public void run() {
    while (true) {
        k.getServingsFromPot();
        try {Thread.sleep(500); // eat
        } catch (Exception e) { return;}
    }
  }
  }
class Cook extends Thread {
  Kitchen k;
  Cook(Kitchen k) {
    this.k = k; 
  }
  public void run() {
    while (true) {
        try {Thread.sleep(500); // sleep
        } catch (Exception e) {return;}
        k.putServingsInPot(10); // 10 servings
    }
  }
}    

public class main {

  public static void main(String Args[]) {
    // Kitchen testing
    Kitchen k = new Kitchen();
    Cook c = new Cook(k);
    c.start();
    Savage sc[] = new Savage[9];
    for (int i = 0; i < 9; i++) {
        sc[i] = new Savage(k); 
        sc[i].start();
    }
    try {
        Thread.sleep(5000);
    } catch (Exception e) {
    }
    for (int i = 0; i < 9; i++) {
        sc[i].interrupt();
    }
    c.interrupt();
    System.out.println("Done\n");
  }

}

Is it possible to synchronized these events without using a semaphore within the monitor ?

3rdeye7
  • 536
  • 4
  • 25
  • Mmm but you change the question! My answer was for your first questions, roll back it, because I solved the first problem, this is a new problem and needs a new question! Please consider this in order to be more clean for the next users to come and read – developer_hatch Nov 12 '17 at 01:27

1 Answers1

1

Look at the definition of Kitchen:

class Kitchen {
  Kitchen k; // Here the kitchen is null
  int c;
  boolean empty;
  Cook chef;
  Kitchen() {
    this.c = 0;
    this.empty = true;
    chef = new Cook(k); // here you give a null object to the cook constructor

  }

you are giving a null object to the Cook constructor. Maybe you want to give yourself to the Cook object:

class Kitchen {
  //Kitchen k; I don't think you will need it anymore, you can delete this line
  int c;
  boolean empty;
  Cook chef;
  Kitchen() {
    this.c = 0;
    this.empty = true;
    chef = new Cook(this); // give yourself to the Cook

  }
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • So by essentially you're passing the instance of the class object to cook as a reference? – 3rdeye7 Nov 12 '17 at 00:51
  • @3rdeye7 exactly, the instance you are creating, but I don't really know what are you trying to do so... I just mentioned the possibility – developer_hatch Nov 12 '17 at 00:52