Skip navigation links

Package javascalautils.concurrent

Contains utilities for concurrent/asynchronous programming.

See: Description

Package javascalautils.concurrent Description

Contains utilities for concurrent/asynchronous programming.
The core principle is the Promise/Future pattern which is the way to separate the execution/job side (Promise) from the client/receiver side (Future).

Promise/Future

In an essence the Promise is the promise to deliver a value at some time in the future.
This is the handle the actual computation side of of the job uses.

The Future is the bearer of a value-to-be, an execution yet to be finished.
The purpose is to have a placeholder for a value from an asynchronous computation.
The preferred way to get hold of the value-to-be is to register a listener on any of the provided listener types since this allows for asynchronous non-blocking operations.
The following image outlines an example of using a Promise/Future pair.

flowchart

Executing a function

In addition to manually using the Executor class for performing asynchronous computations one can let the Future class deal with that automatically.
This is done by using Future.apply or statically importing the FutureCompanion.Future method from the companion class to the Future.
 import static javascalautils.concurrent.FutureCompanion.Future;
 
 Future<Integer> resultSuccess = Future(() -> 9 / 3); // The Future will at some point contain: Success(3)
 Future<Integer> resultFailure = Future(() -> 9 / 0); // The Future will at some point contain: Failure(ArithmeticException)
 
The execution path for the above example is illustrated in the picture below.

flowchart

Refer to the Javadoc for the classes or the Wiki for more details and examples:
https://github.com/pnerg/java-scala-util
Author:
Peter Nerg
Skip navigation links

Copyright © 2015, Peter Nerg Apache License v2.0