////////////////////////////////////////////////////////////////
// CSE 428, SPRING 2000, HW 6 : SOLUTION 1
////////////////////////////////////////////////////////////////


package concurrency.semaphore;


////////////////////////////////////////////////////////////////
// The Semaphore Class
// up() is the V operation
// down() is the P operation
///////////////////////////////////////////////////////////////

public class Semaphore {

    private int value;
    private int max;

    public Semaphore (int initial) {
        value = initial;
        max = initial;
    }

    synchronized public void up() {
        ++value;
        if (value>max) 
           System.out.println("error"); //up() without previous down()
        notifyAll();  //could be notify() but does not work in some browsers
    }

    synchronized public void down() throws InterruptedException {
        while (value==0) wait();
        --value;
    }
}


///////////////////////////////////////////////////////////////

