org.metasyntactic.thread.concurrent
Class Semaphore

java.lang.Object
  |
  +--org.metasyntactic.thread.concurrent.Semaphore
All Implemented Interfaces:
Monitor
Direct Known Subclasses:
QueuedSemaphore, WaiterPreferenceSemaphore

public class Semaphore
extends java.lang.Object
implements Monitor

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.

Different implementation subclasses may provide different ordering guarantees (or lack thereof) surrounding which threads will be resumed upon a signal.

The default implementation makes NO guarantees about the order in which threads will acquire permits. It is often faster than other implementations.


Field Summary
protected  long permits
          current number of available permits
 
Fields inherited from interface org.metasyntactic.thread.concurrent.Monitor
ONE_CENTURY, ONE_DAY, ONE_HOUR, ONE_MINUTE, ONE_SECOND, ONE_WEEK, ONE_YEAR
 
Constructor Summary
Semaphore(long permits)
          Create a Semaphore with the given initial number of permits.
 
Method Summary
 void acquire()
          Wait until a permit is available, and take one
 boolean attempt(long msecs)
          Wait at most msecs millisconds for a permit.
 long permits()
          Return the current number of available permits.
 void release()
          Release a permit
 void release(long n)
          Release N permits.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

permits

protected long permits
current number of available permits

Constructor Detail

Semaphore

public Semaphore(long permits)
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:
permits - The number of permits to grant. Note! This number can be negative, in which case no permits will be released intil the number of releases pushes this above zero
Method Detail

acquire

public void acquire()
             throws java.lang.InterruptedException
Wait until a permit is available, and take one

Specified by:
acquire in interface Monitor
java.lang.InterruptedException

attempt

public boolean attempt(long msecs)
                throws java.lang.InterruptedException
Wait at most msecs millisconds for a permit.

Specified by:
attempt in interface Monitor
Parameters:
msecs - The number of milliseconds to wait for a permit
Returns:
True/False on whether or not the permit was granted
Throws:
java.lang.InterruptedException

release

public void release()
Release a permit

Specified by:
release in interface Monitor

release

public void release(long n)
Release N permits. release(n) is equivalent in effect to:
  for (int i = 0; i < n; ++i) {
	release();
  }
 

Parameters:
n - The number of permits to release
Throws:
java.lang.IllegalArgumentException - if n is negative.

permits

public long permits()
Return the current number of available permits. Returns an accurate, but possibly unstable value, that may change immediately after returning.

Returns:
The number of permits available.