org.jbpm.util
Class Semaphore

java.lang.Object
  extended by org.jbpm.util.Semaphore
All Implemented Interfaces:
java.io.Serializable

public class Semaphore
extends java.lang.Object
implements java.io.Serializable

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.

See Also:
java.util.concurrent.Semaphore, Serialized Form

Constructor Summary
Semaphore(int permits)
          Creates a Semaphore with the given number of permits.
 
Method Summary
 void acquire()
          Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.
 void acquire(int permits)
          Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is interrupted.
 int drainPermits()
          Acquire and return all permits that are immediately available.
 void release()
          Releases a permit, returning it to the semaphore.
 void release(int permits)
          Releases the given number of permits, returning them to the semaphore.
 boolean tryAcquire(int permits, long timeout)
          Acquires the given number of permits from this semaphore, if all become available within the given waiting time and the current thread has not been interrupted.
 boolean tryAcquire(long timeout)
          Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been interrupted.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Semaphore

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

Parameters:
permits - the initial number of permits available. This value may be negative, in which case releases must occur before any acquires will be granted.
Method Detail

release

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


release

public void release(int permits)
Releases the given number of permits, returning them to the semaphore.

Parameters:
permits - the number of permits to release
Throws:
java.lang.IllegalArgumentException - if permits less than zero.

acquire

public void acquire()
             throws java.lang.InterruptedException
Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.

Throws:
java.lang.InterruptedException - if the current thread is interrupted
See Also:
Thread.interrupt()

acquire

public void acquire(int permits)
             throws java.lang.InterruptedException
Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is interrupted.

Parameters:
permits - the number of permits to acquire
Throws:
java.lang.InterruptedException - if the current thread is interrupted
java.lang.IllegalArgumentException - if permits less than zero.
See Also:
Thread.interrupt()

tryAcquire

public boolean tryAcquire(long timeout)
                   throws java.lang.InterruptedException
Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been interrupted.

Parameters:
timeout - the maximum time to wait for a permit
Returns:
true if a permit was acquired and false if the waiting time elapsed before a permit was acquired.
Throws:
java.lang.InterruptedException - if the current thread is interrupted
See Also:
Thread.interrupt()

tryAcquire

public boolean tryAcquire(int permits,
                          long timeout)
                   throws java.lang.InterruptedException
Acquires the given number of permits from this semaphore, if all become available within the given waiting time and the current thread has not been interrupted.

Parameters:
permits - the number of permits to acquire
timeout - the maximum time to wait for the permits
Returns:
true if all permits were acquired and false if the waiting time elapsed before all permits were acquired.
Throws:
java.lang.InterruptedException - if the current thread is interrupted
java.lang.IllegalArgumentException - if permits less than zero.
See Also:
Thread.interrupt()

drainPermits

public int drainPermits()
Acquire and return all permits that are immediately available.

Returns:
the number of permits


Copyright © 2009 JBoss, a division of Red Hat. All Rights Reserved.