![]() | ![]() | ![]() | Futures | Contents | Index |
The ReplyReactor and Actor traits support result-bearing
message send operations (the !! methods) that immediately
return a future. A future, that is, an instance of the
Future
trait, is a handle that can be used to retrieve the response to such a
message send-with-future.
The sender of a message send-with-future can wait for the future's
response by applying the future. For example, sending a message
using val fut = a !! msg allows the sender to wait for the
result of the future as follows: val res = fut().
In addition, a Future can be queried to find out whether its
result is available without blocking using the isSet method.
A message send-with-future is not the only way to obtain a
future. Futures can also be created from computations using the
future method. In the following example, the computation
body is started to run concurrently, returning a future for its
result:
val fut = future { body }
// ...
fut() // wait for future
What makes futures special in the context of actors is the possibility
to retrieve their result using the standard actor-based receive
operations, such as receive etc. Moreover, it is possible to
use the event-based operations react and
reactWithin. This enables an actor to wait for the result of a
future without blocking its underlying thread.
The actor-based receive operations are made available through the
future's inputChannel. For a future of type Future[T],
its type is InputChannel[T]. Example:
val fut = a !! msg
// ...
fut.inputChannel.react {
case Response => // ...
}
![]() | ![]() | ![]() | Futures | Contents | Index |