liveBook · Manning

Future

As explained in Functional Programming in Scala, Second Edition

Future is a Java concurrency interface representing the result of an asynchronous computation and supporting result retrieval and cancellation.

A Future represents the eventual result of asynchronous computation. The ordinary java.util.concurrent.Future exposes get, so retrieving its value can block the current thread; a non-blocking design instead registers a callback for the result. section 7.3.4

opaque type Future[+A] = (A => Unit) => Unit        #1
opaque type Par[+A] = ExecutorService => Future[A]  #2

The callback encoding makes the result opaque: applying an ExecutorService produces a function that accepts an A => Unit continuation. section 7.3.4

The callback-based representation supports several layers of asynchronous composition:

  • fork schedules deferred computation on an ExecutorService without calling a blocking method. section 7.3.4

Running a large parallel computation

A non-blocking Par can map math.sqrt across List.range(1, 100000) and later run the computation on a fixed thread pool, producing a List[Double].

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines callback encoding, executor scheduling, parallel combination, and a blocking run bridge.
Examples
Uses parallel square-root mapping, callback-based byte reads, and console programs compiled into Par.
What it assumes
Assumes ExecutorService and callback functions while demonstrating how blocking Future retrieval becomes non-blocking composition.
What you can do after
Build it from scratch as a callback-based result type and adapt asynchronous sources into composable Par actions.
How it connects
Connects Future with Par, ExecutorService, callbacks, continuations, actors, Free, and asynchronous I/O.

Also explained in

Compare how 8 books explain future →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.