Class/Object

com.twitter.concurrent

AsyncSemaphore

Related Docs: object AsyncSemaphore | package concurrent

Permalink

class AsyncSemaphore extends AnyRef

An AsyncSemaphore is a traditional semaphore but with asynchronous execution.

Grabbing a permit returns a Future[Permit].

Basic usage:

val semaphore = new AsyncSemaphore(n)
...
semaphore.acquireAndRun() {
  somethingThatReturnsFutureT()
}

Calls to acquire() and acquireAndRun are serialized, and tickets are given out fairly (in order of arrival).

See also

AsyncMutex for a mutex version.

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. AsyncSemaphore
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new AsyncSemaphore(initialPermits: Int, maxWaiters: Int)

    Permalink

    Constructs a semaphore with maxWaiters as the limit on the number of waiters for permits.

    Constructs a semaphore with maxWaiters as the limit on the number of waiters for permits.

    initialPermits

    must be positive

    maxWaiters

    must be non-negative

  2. new AsyncSemaphore(initialPermits: Int)

    Permalink

    Constructs a semaphore with no limit on the max number of waiters for permits.

    Constructs a semaphore with no limit on the max number of waiters for permits.

    initialPermits

    must be positive

  3. new AsyncSemaphore(initialPermits: Int, maxWaiters: Option[Int])

    Permalink
    Attributes
    protected

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def acquire(): Future[Permit]

    Permalink

    Acquire a Permit, asynchronously.

    Acquire a Permit, asynchronously. Be sure to permit.release() in a

    - finally block of your onSuccess callback - ensure block of your future chain

    Interrupting this future is only advisory, and will not release the permit if the future has already been satisfied.

    returns

    a Future[Permit] when the Future is satisfied, computation can proceed, or a Future.Exception[RejectedExecutionException] if the configured maximum number of waiters would be exceeded.

    Note

    This method always return the same instance of Permit.

  5. def acquireAndRun[T](func: ⇒ Future[T]): Future[T]

    Permalink

    Execute the function asynchronously when a permit becomes available.

    Execute the function asynchronously when a permit becomes available.

    If the function throws a non-fatal exception, the exception is returned as part of the Future. For all exceptions, the permit would be released before returning.

    returns

    a Future[T] equivalent to the return value of the input function. If the configured maximum value of waitq is reached, Future.Exception[RejectedExecutionException] is returned.

  6. def acquireAndRunSync[T](func: ⇒ T): Future[T]

    Permalink

    Execute the function when a permit becomes available.

    Execute the function when a permit becomes available.

    If the function throws an exception, the exception is returned as part of the Future. For all exceptions, the permit would be released before returning.

    returns

    a Future[T] equivalent to the return value of the input function. If the configured maximum value of waitq is reached, Future.Exception[RejectedExecutionException] is returned.

  7. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  8. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  9. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  10. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  11. def fail(exc: Throwable): Unit

    Permalink

    Fail the semaphore and stop it from distributing further permits.

    Fail the semaphore and stop it from distributing further permits. Subsequent attempts to acquire a permit fail with exc. This semaphore's queued waiters are also failed with exc.

  12. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  14. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  15. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  16. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  17. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  18. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  19. def numInitialPermits: Int

    Permalink
  20. def numPermitsAvailable: Int

    Permalink
  21. def numWaiters: Int

    Permalink
  22. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  23. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  24. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  25. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  26. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped