org.eigenbase.runtime
Class Semaphore

java.lang.Object
  extended by org.eigenbase.runtime.Semaphore

public class Semaphore
extends Object

A counting semaphore. 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, potentially releasing a blocking acquirer. However, no actual permit objects are used; the Semaphore just keeps a count of the number available and acts accordingly.

Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource.

Note that JDK 1.5 contains a Semaphore class. We should obsolete this class when we upgrade.

Version:
$Id: //open/dev/farrago/src/org/eigenbase/runtime/Semaphore.java#9 $
Author:
jhyde

Field Summary
private  int count
           
private static boolean verbose
           
 
Constructor Summary
Semaphore(int count)
          Creates a Semaphore with the given number of permits.
 
Method Summary
 void acquire()
          Acquires a permit from this semaphore, blocking until one is available.
 void release()
          Releases a permit, returning it to the semaphore.
 boolean tryAcquire(long timeoutMillisec)
          Acquires a permit from this semaphore, if one becomes available within the given waiting time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

verbose

private static final boolean verbose
See Also:
Constant Field Values

count

private int count
Constructor Detail

Semaphore

public Semaphore(int count)
Creates a Semaphore with the given number of permits.

Method Detail

acquire

public void acquire()
Acquires a permit from this semaphore, blocking until one is available.


tryAcquire

public boolean tryAcquire(long timeoutMillisec)
Acquires a permit from this semaphore, if one becomes available within the given waiting time.

If timeoutMillisec is less than or equal to zero, does not wait at all.


release

public void release()
Releases a permit, returning it to the semaphore.