////////////////////////////////////////////////////// // Vote.java // Working with Java threads and Semaphores // See commented-out definition of Semaphore at bottom ////////////////////////////////////////////////////// import java.util.*; import java.util.concurrent.Semaphore; import java.util.concurrent.CyclicBarrier; import java.io.*; import java.lang.Thread; public class Vote implements Runnable { private Thread[] child; private final int nrThreads = 5; private Semaphore sem; // semaphore ensures no more than n in voting booth private CyclicBarrier barrier; private String voteText; // record of what has happened private int done; // tells everyone when program is complete public void set_done() { done=1; System.out.println("Parent interrupt"); } private int count=0; // count of voters in the voting booth public Vote() { // initialize semaphore to allow only 1 in the voting booth sem = new Semaphore(1); voteText = new String(); barrier = new CyclicBarrier(nrThreads); // create 5 threads child = new Thread[nrThreads]; done = 0; for (int i=0; i