/* * CISC422/853: Simulation for Assignment 1 * J. Dingel * January 2009 */ class Worker extends Thread { public static final int N = 4; // number of workers public static final int NUM_ROUNDS = 100; // number of simulation rounds private static volatile int[] rank = new int[N]; private static volatile int[] last = new int[N]; private static volatile boolean have_boss = false; static { for(int i=0; i < N; i++) { rank[i] = -1; } for(int i=0; i < N-1; i++) { last[i] = -1; } } private int i; private Timer timer; public Worker (int i) { this.i = i; this.timer = new Timer(); start(); } public void run() { int round = 0; while (round= r && last[r] == i) {} } } } assert !have_boss : "Worker "+i+": Violating the Single-Boss property"; // worker is boss have_boss = true; timer.end(); System.out.println("Worker "+i+": Round: "+round+" Rank: "+N+" Boss! Time required: "+timer.printDuration()); timer.reset(); have_boss = false; // worker stops being boss rank[i] = 0; System.out.println("Worker "+i+": Round: "+round+" Rank: 0"); } } } public class SleeplessCode { public static void main(String[] args) throws InterruptedException { Worker[] Workers = new Worker[Worker.N]; System.out.println("Start simulation: "+Worker.N+" workers. "+Worker.NUM_ROUNDS+" rounds"); for (int i=0; i < Worker.N; ++i) { Workers[i] = new Worker(i); } for (int i=0; i < Worker.N; ++i) { Workers[i].join(); } System.out.println("End of simulation"); } }