The Java Program: Kitchen1.java

  1 // Kitchen1.java -- locking the kitchen reduces potential parallelism
  2 
  3 public class Kitchen1 {
  4 
  5    //  In this kitchen only one measuring cup and one mixing bowl
  6    static MeasuringCup cup = new MeasuringCup();
  7    static MixingBowl  bowl = new MixingBowl();
  8 
  9    public synchronized void makeCookies () {
 10       cup.put (2, "flour");
 11       bowl.put (cup);
 12       bowl.mix ();
 13       // Use oven
 14    }
 15 
 16    public synchronized void makeOmelette () {
 17       bowl.put ("eggs");
 18       bowl.mix ();
 19       cup.put (bowl);
 20       // Use range
 21    }
 22 
 23    // sample main program using the kitchen
 24    public static void main (String args[]) {
 25       final Kitchen1 k = new Kitchen1 ();
 26       class SwedishChef implements Runnable {
 27          public void run () { k.makeCookies(); }
 28       }
 29       new Thread (new SwedishChef(), "Swedish Chef").start();
 30       class JuliaChild implements Runnable {
 31          public void run () { k.makeOmelette(); }
 32       }
 33       new Thread (new JuliaChild(), "Julia Child").start();
 34    }
 35 }