BSsync
Class Semaphore

java.lang.Object
  extended by BSsync.Semaphore

public class Semaphore
extends java.lang.Object

Base class for counting semaphores. Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it. Each release adds a permit. However, no actual permit objects are used; the Semaphore just keeps a count of the number available and acts accordingly.

A semaphore initialized to 1 can serve as a mutual exclusion lock.

This implementation makes NO guarantees about the order in which threads will acquire permits


Field Summary
protected  int permits_
          current number of available permits
static boolean verbose
          Set this variable to true for debugging output
 
Constructor Summary
Semaphore(int initialPermits)
          Create a Semaphore with the given initial number of permits.
Semaphore(int initialPermits, java.lang.String name)
          Create a named Semaphore with the given initial number of permits.
 
Method Summary
 void acquire()
          Wait until a permit is available, and take one
 void release()
          Release a permit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

verbose

public static boolean verbose
Set this variable to true for debugging output


permits_

protected int permits_
current number of available permits

Constructor Detail

Semaphore

public Semaphore(int initialPermits)
Create a Semaphore with the given initial number of permits. Using a seed of one makes the semaphore act as a mutual exclusion lock. Negative seeds are also allowed, in which case no acquires will proceed until the number of releases has pushed the number of permits past 0.

Parameters:
initialPermits - initial value of the Semaphore

Semaphore

public Semaphore(int initialPermits,
                 java.lang.String name)
Create a named Semaphore with the given initial number of permits. the name is only used for debugging output. Using a seed of one makes the semaphore act as a mutual exclusion lock. Negative seeds are also allowed, in which case no acquires will proceed until the number of releases has pushed the number of permits past 0.

Parameters:
initialPermits - initial value of the Semaphore
name - name of the Semaphore
Method Detail

acquire

public void acquire()
Wait until a permit is available, and take one


release

public void release()
Release a permit