Promises
In Chapter 2, Concurrency on the JVM and the Java Memory Model, we implemented an asynchronous
method that used a worker thread and a task queue to receive and execute asynchronous computations. That example should have left you with a basic intuition about how the execute
method is implemented in execution contexts. You might be wondering how the Future.apply
method can return and complete a Future
object. We will study promises in this section to answer this question. Promises are objects that can be assigned a value or an exception only once. This is why promises are sometimes also called single-assignment variables. A promise is represented with the Promise[T]
type in Scala. To create a promise instance, we use the Promise.apply
Get Learning Concurrent Programming in Scala now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.