public class Worker extends Thread { private Semaphore mutex; private int id; private final static int LOOP_COUNT = 5000000; public Worker(Semaphore s, int id) { super("Worker"+id); this.mutex = s; this.id = id; } public int getId() { return id; } public void run() { while (true) { System.out.println("worker " + getId() + " waiting on sema"); // enter the critical section System.out.println("worker " + getId() + " entering cs"); doWork(); System.out.println("worker " + getId() + " signaling sema"); // leave the critical section System.out.println("worker " + getId() + " leaving cs"); yield(); } } private void doWork() { System.out.println("worker " + getId() + " working "); for (int i = 0; i < LOOP_COUNT; i ++); } }