net.sf.asyncobjects.util
Class Semaphore

java.lang.Object
  extended by net.sf.asyncobjects.AsyncUnicastServer<ASemaphore>
      extended by net.sf.asyncobjects.util.Semaphore
All Implemented Interfaces:
AsyncObject, ExplicitSharing, ASemaphore

public class Semaphore
extends AsyncUnicastServer<ASemaphore>
implements ASemaphore

Asynchronous semaphore service. It works both throw proxy and when used directly in the single thread. This class maintains positive and negative permits separtely. Positive permits is what is avaialbe for the current aquire call. Negative permits is what is scheduled.

Author:
const

Constructor Summary
Semaphore(int permits)
          Create semaphore with specified number of permits.
 
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 ASemaphore.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 class net.sf.asyncobjects.AsyncUnicastServer
dereference, export, isImmediate, myVat, promise
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.asyncobjects.AsyncObject
dereference, isImmediate
 

Constructor Detail

Semaphore

public Semaphore(int permits)
Create semaphore with specified number of permits. It could be negative.

Parameters:
permits - amount of permits.
Method Detail

acquire

public Promise<Void> acquire()
Specified by:
acquire in interface ASemaphore
Returns:
when aquired one permit
See Also:
ASemaphore.acquire()

acquire

public Promise<Void> acquire(int number)
Description copied from interface: ASemaphore
Aquiree a number of permits

Specified by:
acquire in interface ASemaphore
Parameters:
number - amount of permits to aquired
Returns:
when permits are aquired
See Also:
ASemaphore.acquire(int)

availablePermits

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

Specified by:
availablePermits in interface ASemaphore
Returns:
amount of currently available permits.
See Also:
ASemaphore.availablePermits()

permitsBalance

public Promise<Integer> permitsBalance()
Specified by:
permitsBalance in interface ASemaphore
Returns:
return an permits balance. The sum of permits requested by pending ASemaphore.acquire(int) calls is substracted from available permits.
See Also:
ASemaphore.permitsBalance()

drainPermits

public Promise<Integer> drainPermits()
Description copied from interface: ASemaphore
Drain all available permits. Note that this method waits until at least zero permits available. If less than zero permits available, the method blocks.

Specified by:
drainPermits in interface ASemaphore
Returns:
amount of permits that were available.
See Also:
ASemaphore.drainPermits()

release

public void release()
Description copied from interface: ASemaphore
Release a permit

Specified by:
release in interface ASemaphore
See Also:
ASemaphore.release()

release

public void release(int number)
Description copied from interface: ASemaphore
Release a number of permits

Specified by:
release in interface ASemaphore
Parameters:
number - a number of permits
See Also:
ASemaphore.release(int)


Copyright © 2002-2007 Constantine Plotnikov. All Rights Reserved.