Futures and blocking IO
The choice of using ForkJoinPool
for imminent is deliberate. ForkJoinPool
, which was added in Java 7, is extremely smart. When created, you give it a desired level of parallelism, which defaults to the number of available processors.
ForkJoinPool
then attempts to honor this desired parallelism by dynamically shrinking and expanding the pool as required. When a task is submitted to this pool, it doesn't necessarily create a new thread if it doesn't have to. This allows the pool to serve an extremely large number of tasks with a much smaller number of actual threads.
However, it cannot guarantee such optimizations in the face of blocking IO, as it can't know whether the thread is blocking, waiting for an external resource. Nevertheless, ForkJoinPool
provides a mechanism by which threads can notify the pool when they might block.
Imminent takes advantage of this mechanism by implementing the ManagedBlocker
interface [2], and provides another way to create futures, as demonstrated...