/** * Semaphore.java * * A basic counting semaphore using Java synchronization. * */ public final class Semaphore { private int count; public Semaphore() { count = 0; } public Semaphore(int value) { count = value; } public synchronized void P() { while (count <= 0) { System.out.println("Thread " + Thread.currentThread().getName() +" waits in P-Operation!"); try { wait(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } count--; } public synchronized void V() { ++count; notify(); } public int getCounter() { return count; } }