public class Semaphore implements /*java.io.*/Serializable {
private static final long serialVersionUID = -3222578661600680210L;
+ private final int count;
+ private final boolean fair;
/** All mechanics via AbstractQueuedSynchronizer subclass */
//private final Sync sync;
*/
public Semaphore(int permits) {
//sync = new NonfairSync(permits);
+ count = permits;
}
/**
*/
public Semaphore(int permits, boolean fair) {
//sync = (fair)? new FairSync(permits) : new NonfairSync(permits);
+ count = permits;
+ fair = fair;
}
/**
*
* @throws InterruptedException if the current thread is interrupted
*/
- public void acquire() throws InterruptedException {
+ public synchronized void acquire() throws InterruptedException {
//sync.acquireSharedInterruptibly(1);
- System.out.println("Unimplemented Semaphore.acquire()!");
+ //System.out.println("Unimplemented Semaphore.acquire()!");
+ while(this.count == 0) wait();
+ this.count--;
+ this.notify();
}
/**
* Correct usage of a semaphore is established by programming convention
* in the application.
*/
- public void release() {
+ public synchronized void release() {
//sync.releaseShared(1);
- System.out.println("Unimplemented Semaphore.release()!");
+ //System.out.println("Unimplemented Semaphore.release()!");
+ this.count++;
+ this.notify();
}
/**
public void acquire(int permits) throws InterruptedException {
/*if (permits < 0) throw new IllegalArgumentException();
sync.acquireSharedInterruptibly(permits);*/
- System.out.println("Unimplemented Semaphore.acquire(int)!");
+ //System.out.println("Unimplemented Semaphore.acquire(int)!");
+ while(this.count < permits) wait();
+ this.count-=permits;
+ this.notify();
}
/**
public void release(int permits) {
/*if (permits < 0) throw new IllegalArgumentException();
sync.releaseShared(permits);*/
- System.out.println("Unimplemented Semaphore.release()!");
+ //System.out.println("Unimplemented Semaphore.release()!");
+ this.count+=permits;
+ this.notify();
}
/**