// An implementation of a general semaphore adapted from // that given in Doug Lea's book on concurrent programming in Java. // A while(condition) {..wait()..} loop is used to suspend the process // invoking a P(). class semaphore2 { private int value ; public semaphore2(int i) { value = i ; } synchronized void P() { while ( value <= 0 ) try { wait() ; } catch (InterruptedException e) { } value-- ; } synchronized void V() { value++ ; notify() ; } }