package concurrent
- Alphabetic
- Public
- Protected
Type Members
- abstract class AbstractOffer[T] extends Offer[T]
Abstract
Offer
class for Java compatibility. - class AsyncMeter extends AnyRef
An asynchronous meter.
An asynchronous meter.
Processes can create an asynchronously awaiting future, a "waiter" to wait until the meter allows it to continue, which is when the meter can give it as many permits as it asked for. Up to
burstSize
permits are issued everyburstDuration
. IfmaxWaiters
waiters are enqueued simultaneously, it will reject further attempts to wait, until some of the tasks have been executed.It may be appropriate to use this to smooth out bursty traffic, or if using a resource that's rate-limited based on time. However, to avoid overwhelming a constrained resource that doesn't exert coordination controls like backpressure, it's safer to limit based on AsyncSemaphore, since it can speed up if that resource speeds up, and slow down if that resource slows down.
// create a meter that allows 1 operation per second, and a max of 1000 waiting val meter = new AsyncMeter(1, 1.second, 1000) def notMoreThanOncePerSecond[A](f: => Future[A]): Future[A] = { meter.await(1).handle { case t: RejectedExecutionException => // do something else when too many waiters }.before { f } }
- class AsyncMutex extends AsyncSemaphore
An AsyncMutex is a traditional mutex but with asynchronous execution.
An AsyncMutex is a traditional mutex but with asynchronous execution.
Basic usage:
val mutex = new AsyncMutex() ... mutex.acquireAndRun() { somethingThatReturnsFutureT() }
- See also
AsyncSemaphore for a semaphore version.
- class AsyncQueue[T] extends AnyRef
An asynchronous FIFO queue.
- class AsyncSemaphore extends AnyRef
An AsyncSemaphore is a traditional semaphore but with asynchronous execution.
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.
- sealed abstract class AsyncStream[+A] extends AnyRef
A representation of a lazy (and possibly infinite) sequence of asynchronous values.
A representation of a lazy (and possibly infinite) sequence of asynchronous values. We provide combinators for non-blocking computation over the sequence of values.
It is composable with Future, Seq and Option.
val ids = Seq(123, 124, ...) val users = fromSeq(ids).flatMap(id => fromFuture(getUser(id))) // Or as a for-comprehension... val users = for { id <- fromSeq(ids) user <- fromFuture(getUser(id)) } yield user
All of its operations are lazy and don't force evaluation, unless otherwise noted.
The stream is persistent and can be shared safely by multiple threads.
- class BridgedThreadPoolScheduler extends Scheduler with ExecutorScheduler
A scheduler that bridges tasks submitted by external threads into local executor threads.
A scheduler that bridges tasks submitted by external threads into local executor threads. All tasks submitted locally are executed on local threads.
Note: This scheduler expects to create executors with unbounded capacity. Thus it does not expect and has undefined behavior for any
RejectedExecutionException
s other than those encountered after executor shutdown. - class Broker[T] extends AnyRef
An unbuffered FIFO queue, brokered by
Offer
s.An unbuffered FIFO queue, brokered by
Offer
s. Note that the queue is ordered by successful operations, not initiations, soone
andtwo
may not be received in that order with this code:val b: Broker[Int] b ! 1 b ! 2
But rather we need to explicitly sequence them:
val b: Broker[Int] for { () <- b ! 1 () <- b ! 2 } ()
BUGS: the implementation would be much simpler in the absence of cancellation.
- trait ExecutorScheduler extends AnyRef
A named Scheduler mix-in that causes submitted tasks to be dispatched according to an
java.util.concurrent.ExecutorService
created by an abstract factory function. - trait ForkingScheduler extends Scheduler
A scheduler that provides methods to fork the execution of
Future
computations and behaves similarly to a thread pool - class LocalScheduler extends Scheduler
An efficient thread-local, direct-dispatch scheduler.
- class NamedPoolThreadFactory extends ThreadFactory
A
java.util.concurrent.ThreadFactory
which creates threads with a name indicating the pool from which they originated.A
java.util.concurrent.ThreadFactory
which creates threads with a name indicating the pool from which they originated.A new
java.lang.ThreadGroup
(namedname
) is created as a sub-group of whichever group to which the thread that created the factory belongs. Each thread created by this factory will be a member of this group and have a unique name including the group name and an monotonically increasing number. The intention of this naming is to ease thread identification in debugging output.For example, a
NamedPoolThreadFactory
withname="writer"
will create aThreadGroup
named "writer" and new threads will be named "writer-1", "writer-2", etc. - trait Offer[+T] extends AnyRef
An offer to communicate with another process.
An offer to communicate with another process. The offer is parameterized on the type of the value communicated. An offer that sends a value typically has type {{Unit}}. An offer is activated by synchronizing it, which is done with
sync()
.Note that Offers are persistent values -- they may be synchronized multiple times. They represent a standing offer of communication, not a one-shot event.
The synchronization protocol
Synchronization is performed via a two-phase commit process.
prepare()
commences the transaction, and when the other party is ready, it returns with a transaction object,Tx[T]
. This must then be ackd or nackd. If both parties acknowledge,Tx.ack()
returns with a commit object, containing the value. This finalizes the transaction. Please see theTx
documentation for more details on that phase of the protocol.Note that a user should never perform this protocol themselves -- synchronization should always be done with
sync()
.Future interrupts are propagated, and failure is passed through. It is up to the implementer of the Offer to decide on failure semantics, but they are always passed through in all of the combinators.
Note: There is a Java-friendly API for this trait: com.twitter.concurrent.AbstractOffer.
- final class Offers extends AnyRef
A Java adaptation of
com.twitter.concurrent.Offer
companion object. - trait Permit extends AnyRef
Token representing an interest in a resource and a way to release that interest.
- trait Scheduler extends AnyRef
An interface for scheduling
java.lang.Runnable
tasks. - trait Serialized extends AnyRef
Efficient ordered serialization of operations.
Efficient ordered serialization of operations.
Note: This should not be used in place of Scala's
synchronized
, but rather only when serialization semantics are required. - sealed trait Spool[+A] extends AnyRef
Note: Spool is no longer the recommended asynchronous stream abstraction.
Note: Spool is no longer the recommended asynchronous stream abstraction. We encourage you to use AsyncStream instead. See Spools for the Java adaptation of the Spool companion object.
A spool is an asynchronous stream. It more or less mimics the scala {{Stream}} collection, but with cons cells that have either eager or deferred tails.
Construction of eager Spools is done with either Spool.cons or the {{**::}} operator. To construct a lazy/deferred Spool which materializes its tail on demand, use the {{*::}} operator. In order to use these operators for deconstruction, they must be imported explicitly (ie: {{import Spool.{*::, **:: }}} )
def fill(rest: Promise[Spool[Int]]) { asyncProcess foreach { result => if (result.last) { rest() = Return(result **:: Spool.empty) } else { val next = new Promise[Spool[Int]] rest() = Return(result *:: next) fill(next) } } } val rest = new Promise[Spool[Int]] fill(rest) firstElem *:: rest
explicitly (ie: {{import Spool.{*::, **:: }}}
def fill(rest: Promise[Spool[Int]]) { asyncProcess foreach { result => if (result.last) { rest() = Return(result **:: Spool.empty) } else { val next = new Promise[Spool[Int]] rest() = Return(result *:: next) fill(next) } } } val rest = new Promise[Spool[Int]] fill(rest) firstElem *:: rest
- class SpoolSource[A] extends AnyRef
A SpoolSource is a simple object for creating and populating a Spool-chain.
A SpoolSource is a simple object for creating and populating a Spool-chain. apply() returns a Future[Spool] that is populated by calls to offer(). This class is thread-safe.
- final class Spools extends AnyRef
A Java adaptation of
com.twitter.concurrent.Spool
companion object. - class ThreadPoolScheduler extends Scheduler with ExecutorScheduler
A scheduler that dispatches directly to an underlying Java cached threadpool executor.
- trait Tx[+T] extends AnyRef
A
Tx
is used to mediate multi-party transactions with the following protocol:A
Tx
is used to mediate multi-party transactions with the following protocol:- A transaction is complete when all parties have completed acknowledgment. 2. If any party aborts (nack), the entire transaction is considered aborted. 3. Once a transaction has been acknowledged by a party, that acknowledgment must be honored: The party cannot subsequently nack.
- final class Txs extends AnyRef
A Java adaptation of
com.twitter.concurrent.Tx
companion object.
Value Members
- object AsyncMeter
- object AsyncQueue
- object AsyncSemaphore
- object AsyncStream
- object ForkingScheduler
- object Offer
Note: There is a Java-friendly API for this object: com.twitter.concurrent.Offers.
- object Once
- object Scheduler extends Scheduler
A global scheduler.
- object Spool
Note: Spool is no longer the recommended asynchronous stream abstraction.
Note: Spool is no longer the recommended asynchronous stream abstraction. We encourage you to use AsyncStream instead.
Note: There is a Java-friendly API for this object: com.twitter.concurrent.Spools.
- object SpoolSource
- object Tx
Note: There is a Java-friendly API for this object: com.twitter.concurrent.Txs.