Register a FutureEventListener to be invoked when the computation completes.
Register a FutureEventListener to be invoked when the computation completes. This method is typically used by Java programs because it avoids the use of small Function objects.
Compare this method to transformedBy
. The difference is that
addEventListener
is used to perform a simple action when a
computation completes, such as recording data in a log-file. It
analogous to a void
method in Java: it has side-effects and no
return value. transformedBy
, on the other hand, is used to
transform values from one type to another, or to chain a series
of asynchronous calls and return the result. It is analogous to
methods in Java that have a return-type. Note that
transformedBy
and addEventListener
are not mutually
exclusive and may be profitably combined.
Become the other promise.
Become the other promise. become
declares an equivalence
relation: this
and other
are the same.
By becoming other
, its waitlists are now merged into this
's,
and this
becomes canonical. The same is true of interrupt
handlers: other
's interrupt handler is overwritten with the
handlers installed for this
.
Note: Using become
and setInterruptHandler
on the same
promise is not recommended. Consider the following, which
demonstrates unexpected behavior related to this usage.
val a, b = new Promise[Unit] a.setInterruptHandler { case _ => println("A") } b.become(a) b.setInterruptHandler { case _ => println("B") } a.raise(new Exception)
This prints "B", the action in the interrupt handler for b
,
which is unexpected because we raised on a
. In this case and
others, using com.twitter.util.Future.proxyTo may be more
appropriate.
Note that this
must be unsatisfied at the time of the call,
and not race with any other setters. become
is a form of
satisfying the promise.
This has the combined effect of compressing the other
into
this
, effectively providing a form of tail-call elimination
when used in recursion constructs. transform
(and thus any
other combinator) use this to compress Futures, freeing them
from space leaks when used with recursive constructions.
Note: do not use become with cyclic graphs of futures: the
behavior of racing a.become(b)
with b.become(a)
is undefined
(where a
and b
may resolve as such transitively).
Sequentially compose this
with f
.
Should only be called when this Promise has already been fulfilled
or it is becoming another Future via become
.
Should only be called when this Promise has already been fulfilled
or it is becoming another Future via become
.
Delay the completion of this Future for at least
howlong
from now.
Delay the completion of this Future for at least
howlong
from now.
Invoked regardless of whether the computation completed successfully or unsuccessfully.
Invoked regardless of whether the computation completed successfully or unsuccessfully. Implemented in terms of respond so that subclasses control evaluation order. Returns a chained Future.
The returned Future
will be satisfied when this,
the original future, is done.
the side-effect to apply when the computation completes.
If this, the original future, succeeds, run f
on the result.
If this, the original future, succeeds, run f
on the result.
The returned result is a Future that is satisfied when the original future
and the callback, f
, are done.
If the original future fails, this one will also fail, without executing f
and preserving the failed computation of this
.
Converts a Future[Future[B]] into a Future[B]
Converts a Future[Future[B]] into a Future[B]
Invoke the callback only if the Future returns successfully.
Forward interrupts to another future.
Forward interrupts to another future. If the other future is fulfilled, this is a no-op. Calling this multiple times is not recommended as the resulting state may not be as expected.
the Future to which interrupts are forwarded.
If this, the original future, results in an exceptional computation,
rescueException
may convert the failure into a new result.
If this, the original future, results in an exceptional computation,
rescueException
may convert the failure into a new result.
The returned result is a Future
that is satisfied when the original
future and the callback, rescueException
, are done.
This is the equivalent of map for failed computations.
Makes a derivative Future which will be satisfied with the result of the parent.
Makes a derivative Future which will be satisfied with the result of the parent. However, if it's interrupted, it will detach from the parent Future, satisfy itself with the exception raised to it, and won't propagate the interruption back to the parent Future.
This is useful for when a Future is shared between many contexts, where it may not be useful to discard the underlying computation if just one context is no longer interested in the result. In particular, this is different from Future#masked in that it will prevent memory leaks if the parent Future will never be satisfied, because closures that are attached to this derivative Future will not be held onto by the killer Future.
Is the result of the Future available yet?
Checks whether a Unit-typed Future is done.
Checks whether a Unit-typed Future is done. By convention, futures of type Future[Unit] are used for signalling.
Returns this promise's interrupt if it is interrupted.
Is this Awaitable ready? In other words: would calling Awaitable.ready block?
Is this Awaitable ready? In other words: would calling Awaitable.ready block?
Combines two Futures into one Future of the Tuple of the two results.
Combines two Futures into one Future of the Tuple of the two results.
Returns the result of the computation as a Future[Try[A]].
Returns the result of the computation as a Future[Try[A]].
If this, the original future, succeeds, run f
on the result.
If this, the original future, succeeds, run f
on the result.
The returned result is a Future that is satisfied when the original future
and the callback, f
, are done.
If the original future fails, this one will also fail, without executing f
and preserving the failed computation of this
.
Returns an identical future except that it ignores interrupts which match a predicate
Returns an identical future except that it ignores interrupts which match a predicate
Returns an identical future that ignores all interrupts
Returns an identical future that ignores all interrupts
Invoke the function on the error, if the computation was unsuccessful.
Invoke the function on the error, if the computation was
unsuccessful. Returns a chained Future as in respond
.
chained Future
if fn
is a PartialFunction
and the input is not defined for a given
Throwable, the resulting MatchError
will propogate to the current
Monitor
. This will happen if you use a construct such as
future.onFailure { case NonFatal(e) => ... }
when the Throwable
is "fatal".
this should be used for side-effects.
handle and rescue to produce a new Future
from the result of
the computation.
Invoke the function on the result, if the computation was successful.
A synonym for select: Choose the first Future to be satisfied.
Polls for an available result.
Send updates from this Future to the other.
Send updates from this Future to the other.
other
must not yet be satisfied at the time of the call.
After this call, nobody else should satisfy other
.
Note: using proxyTo will mask interrupts to this future, and it's
the user's responsibility to set an interrupt handler on other
to raise on f. In some cases, using
com.twitter.util.Promise.become may be more appropriate.
Raise the given throwable as an interrupt.
Raise the given throwable as an interrupt. Interrupts are
one-shot and latest-interrupt wins. That is, the last interrupt
to have been raised is delivered exactly once to the Promise
responsible for making progress on the future (multiple such
promises may be involved in flatMap
chains).
Raising an interrupt does not alter the externally observable state of the Future. They are used to signal to the producer of the future's value that the result is no longer desired (for whatever reason given in the passed Throwable).
Returns a new Future that will error if this Future does not return in time.
Returns a new Future that will error if this Future does not return in time.
Note: On timeout, the underlying future is interrupted.
Same as the other raiseWithin, but with an implicit timer.
Same as the other raiseWithin, but with an implicit timer. Sometimes this is more convenient.
Note: On timeout, the underlying future is interrupted.
Same as the other raiseWithin, but with an implicit timer.
Same as the other raiseWithin, but with an implicit timer. Sometimes this is more convenient.
Note: On timeout, the underlying future is interrupted.
Support for Await.ready
.
If this, the original future, results in an exceptional computation,
rescueException
may convert the failure into a new result.
If this, the original future, results in an exceptional computation,
rescueException
may convert the failure into a new result.
The returned result is a Future
that is satisfied when the original
future and the callback, rescueException
, are done.
This is the equivalent of flatMap for failed computations.
Note: exceptions in responds are monitored.
Support for Await.result
.
Choose the first Future to be satisfied.
Choose the first Future to be satisfied.
another Future
a new Future whose result is that of the first of this and other to return
Sets a Unit-typed future.
Sets a Unit-typed future. By convention, futures of type Future[Unit] are used for signalling.
Populate the Promise with the given exception.
Populate the Promise with the given exception.
ImmutableResult
if the Promise is already populated
(Re)sets the interrupt handler.
(Re)sets the interrupt handler. There is only one active interrupt handler.
the new interrupt handler
Populate the Promise with the given result.
Populate the Promise with the given result.
ImmutableResult
if the Promise is already populated
Convert a Twitter Future to a Java native Future.
Convert a Twitter Future to a Java native Future. This should match the semantics of a Java Future as closely as possible to avoid issues with the way another API might use them. See:
http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html#cancel(boolean)
An offer for this future.
An offer for this future. The offer is activated when the future is satisfied.
Transform the Future[A] into a Future[B] using the FutureTransformer.
Transform the Future[A] into a Future[B] using the FutureTransformer. The FutureTransformer handles both success (Return) and failure (Throw) values by implementing map/flatMap and handle/rescue. This method is typically used by Java programs because it avoids the use of small Function objects.
Compare this method to addEventListener
. The difference is
that addEventListener
is used to perform a simple action when
a computation completes, such as recording data in a log-file.
It analogous to a void
method in Java: it has side-effects and
no return value. transformedBy
, on the other hand, is used to
transform values from one type to another, or to chain a series
of asynchronous calls and return the result. It is analogous to
methods in Java that have a return-type. Note that
transformedBy
and addEventListener
are not mutually
exclusive and may be profitably combined.
Note: The FutureTransformer must implement either flatMap
or map
and may optionally implement handle
. Failing to
implement a method will result in a run-time (AbstractMethod)
error.
Convert this Future[A] to a Future[Unit] by discarding the result.
Convert this Future[A] to a Future[Unit] by discarding the result.
Populate the Promise with the given Try.
Populate the Promise with the given Try.
Populate the Promise with the given Try. The Try can either be a
value or an exception. setValue
and setException
are generally
more readable methods to use.
true only if the result is updated, false if it was already set.
Invoking updateIfEmpty
without checking the boolean result is almost
never the right approach. Doing so is generally unsafe unless race
conditions are acceptable.
Convert this Future[A] to a Future[Void] by discarding the result.
Convert this Future[A] to a Future[Void] by discarding the result.
Returns a Future[Boolean] indicating whether two Futures are equivalent.
Returns a Future[Boolean] indicating whether two Futures are equivalent. Note that Future.exception(e).willEqual(Future.exception(e)) == Future.value(true).
Returns a new Future that will error if this Future does not return in time.
Returns a new Future that will error if this Future does not return in time.
Note: On timeout, the underlying future is not interrupted.
to run timeout on.
indicates how long you are willing to wait for the result to be available.
exception to throw.
Returns a new Future that will error if this Future does not return in time.
Returns a new Future that will error if this Future does not return in time.
Note: On timeout, the underlying future is not interrupted.
Same as the other within, but with an implicit timer.
Same as the other within, but with an implicit timer. Sometimes this is more convenient.
Note: On timeout, the underlying future is not interrupted.
Block, but only as long as the given timeout
, for the result
of the Future to be available.
Block, but only as long as the given timeout
, for the result
of the Future to be available.
(Since version 6.2.x) Use Await.result
Block indefinitely, wait for the result of the Future to be available.
Block indefinitely, wait for the result of the Future to be available.
(Since version 6.2.x) Use Await.result
(Since version 6.0.0) Provided for API compatibility; use raise() instead.
Demands that the result of the future be available within
timeout
.
Demands that the result of the future be available within
timeout
. The result is a Return[_] or Throw[_] depending upon
whether the computation finished in time.
(Since version 6.2.x) Use Await.result(future.liftToTry)
Alias for apply().
Alias for apply().
(Since version 6.2.x) Use Await.result
(Since version 6.2.x) Use Await.result(future.liftToTry).isReturn
(Since version 6.2.x) Use Await.result(future.liftToTry).isThrow
A writeable com.twitter.util.Future that supports merging. Callbacks (responders) of Promises are scheduled with com.twitter.concurrent.Scheduler.
Implementation details
A Promise is in one of five states:
Waiting
,Interruptible
,Interrupted
,Done
andLinked
whereInterruptible
andInterrupted
are variants ofWaiting
to deal with future interrupts. Promises are concurrency-safe, using lock-free operations throughout. Callback dispatch is scheduled with com.twitter.concurrent.Scheduler.Waiters are stored as a com.twitter.util.Promise.K.
K
s (mnemonic: continuation) specifies adepth
. This is used to implement Promise chaining: a callback with depthd
is invoked only after all callbacks with depth <d
have already been invoked.Promise.become
merges two promises: they are declared equivalent.become
merges the states of the two promises, and links one to the other. Thus promises support the analog to tail-call elimination: no space leak is incurred fromflatMap
in the tail position since intermediate promises are merged into the root promise.A number of optimizations are employed to conserve space: we pay particular heed to the JVM's object representation, in particular for OpenJDK (HotSpot) version 7 running on 64-bit architectures with compressed OOPS. See comments on com.twitter.util.Promise.State for details.