// stand alone version of j.n.magee 11/11/96 // by mh 19/03/03 // only textuell user interface class Counter { int value=0; Counter(int init) { value = init; } void increment() { int temp = value; //read[v] Simulate.interrupt(); value=temp+1; //write[v+1] } int get () { return value; } } // class Counter class Simulate { public static void interrupt() { if (Math.random() < 0.4) try { Thread.sleep(200);} catch(InterruptedException e){}; //used instead of Thread.yield() to ensure portability } } // class Simulate class Turnstile extends Thread { Counter people; Turnstile (Counter c) { people = c; } public void run() { for (int i=1;i<=GardenTui.MAX;i++){ people.increment(); } } } // class Turnstile public class GardenTui { final static int MAX = 20; public static void main (String[] argv) { Counter counter = new Counter(0); Turnstile west= new Turnstile(counter); Turnstile east= new Turnstile(counter); west.start(); east.start(); try { west.join(); east.join(); System.out.println("total count: " + counter.get()); } catch (Exception e) {} } } // class GardenTui