net.sf.asyncobjects.util
Interface ASemaphore

All Superinterfaces:
AsyncObject
All Known Implementing Classes:
Semaphore

public interface ASemaphore
extends AsyncObject

This is an asynchronous version of Semaphore. It maintain internal balance of permits. The method release(int) increases the amount of permits and the method acquire(int) reduces the amount. However the method acquire reduces amount of permits and resolves its result promise only when amount of permits will be at least zero after substracting the permits.

The method drainPermits() resets the amount of permits to zero when all previously enqueued acquire(int) completed.

Ivocations of the methods acquire(int) and drainPermits() are ordered. the later invocation will not be executed until the first one is complete. In contrast to it, the release(int) methods are not queued. They are executed as soon as they are received.

Author:
const

Method Summary
 Promise<Void> acquire()
           
 Promise<Void> acquire(int number)
          Aquiree a number of permits
 Promise<Integer> availablePermits()
          This is amount of permits that are released but have not yet been acquired with a acquire(int) call.
 Promise<Integer> drainPermits()
          Drain all available permits.
 Promise<Integer> permitsBalance()
           
 void release()
          Release a permit
 void release(int number)
          Release a number of permits
 
Methods inherited from interface net.sf.asyncobjects.AsyncObject
dereference, isImmediate
 

Method Detail

acquire

Promise<Void> acquire()
Returns:
when aquired one permit

acquire

Promise<Void> acquire(int number)
Aquiree a number of permits

Parameters:
number - amount of permits to aquired
Returns:
when permits are aquired

release

void release()
Release a permit


release

void release(int number)
Release a number of permits

Parameters:
number - a number of permits

availablePermits

Promise<Integer> availablePermits()
This is amount of permits that are released but have not yet been acquired with a acquire(int) call. If there are pending acquire(int) call, this could happen due to several reasons:

Returns:
amount of currently available permits.

permitsBalance

Promise<Integer> permitsBalance()
Returns:
return an permits balance. The sum of permits requested by pending acquire(int) calls is substracted from available permits.

drainPermits

Promise<Integer> drainPermits()
Drain all available permits. Note that this method waits until at least zero permits available. If less than zero permits available, the method blocks.

Returns:
amount of permits that were available.


Copyright 2002-2007 Constantine Plotnikov. All Rights Reserved.