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.
- Alphabetic
- By Inheritance
- AsyncSemaphore
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new AsyncSemaphore(initialPermits: Int, maxWaiters: Int)
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
- new AsyncSemaphore(initialPermits: Int)
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
- new AsyncSemaphore(initialPermits: Int, maxWaiters: Option[Int])
- Attributes
- protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def acquire(): Future[Permit]
Acquire a Permit, asynchronously.
Acquire a Permit, asynchronously. Be sure to
permit.release()
in a-
finally
block of youronSuccess
callback -ensure
block of your future chainInterrupting this future is only advisory, and will not release the permit if the future has already been satisfied.
- returns
a
Future[Permit]
when theFuture
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.
- def acquireAndRun[T](func: => Future[T]): Future[T]
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.
- def acquireAndRunSync[T](func: => T): Future[T]
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.
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def fail(exc: Throwable): Unit
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 withexc
. - def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def numInitialPermits: Int
- def numPermitsAvailable: Int
- def numWaiters: Int
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()