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 }
14
15 public synchronized void makeOmelette () {
16 bowl.put ("eggs");
17 bowl.mix ();
18 cup.put (bowl);
19 }
20
21 // sample main program using the kitchen
22 public static void main (String args[]) {
23 final Kitchen1 k = new Kitchen1 ();
24 class SwedishChef implements Runnable {
25 public void run () { k.makeCookies(); }
26 }
27 new Thread (new SwedishChef(), "Swedish Chef").start();
28 class JuliaChild implements Runnable {
29 public void run () { k.makeOmelette(); }
30 }
31 new Thread (new JuliaChild(), "Julia Child").start();
32 }
33 }