Package play.libs.concurrent
Interface Futures
-
- All Known Implementing Classes:
DefaultFutures
public interface Futures
Utilities for creatingCompletionStage
operations.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description CompletionStage<Done>
delay(long amount, TimeUnit unit)
Creates a completion stage which is only completed after the delay.CompletionStage<Done>
delay(Duration duration)
Creates a completion stage which is only completed after the delay.<A> CompletionStage<A>
delayed(Callable<CompletionStage<A>> callable, long amount, TimeUnit unit)
Create aCompletionStage
which, after a delay, will be redeemed with the result of a given callable.<A> CompletionStage<A>
delayed(Callable<CompletionStage<A>> callable, Duration duration)
Create aCompletionStage
which, after a delay, will be redeemed with the result of a given supplier.static <A> CompletionStage<List<A>>
sequence(Iterable<? extends CompletionStage<A>> promises)
Combine the given CompletionStages into a singleCompletionStage
for the list of results.static <A> CompletionStage<List<A>>
sequence(CompletionStage<A>... promises)
Combine the given CompletionStages into a single CompletionStage for the list of results.<A> CompletionStage<A>
timeout(CompletionStage<A> stage, long amount, TimeUnit unit)
Creates aCompletionStage
that returns either the input stage, or a timeout.<A> CompletionStage<A>
timeout(CompletionStage<A> stage, Duration duration)
-
-
-
Method Detail
-
timeout
<A> CompletionStage<A> timeout(CompletionStage<A> stage, long amount, TimeUnit unit)
Creates aCompletionStage
that returns either the input stage, or a timeout.Note that timeout is not the same as cancellation. Even in case of timeout, the given completion stage will still complete, even though that completed value is not returned.
CompletionStage<Double> callWithTimeout() { return futures.timeout(delayByOneSecond(), Duration.ofMillis(300)); }
- Type Parameters:
A
- the completion's result type.- Parameters:
stage
- the input completion stage that may time out.amount
- The amount (expressed with the corresponding unit).unit
- The time Unit.- Returns:
- either the completed completion stage, or a completion stage that failed with timeout.
-
timeout
<A> CompletionStage<A> timeout(CompletionStage<A> stage, Duration duration)
- Type Parameters:
A
- the completion stage that should be wrapped with a timeout.- Parameters:
stage
- the input completion stage that may time out.duration
- The duration after which there is a timeout.- Returns:
- the completion stage, or a completion stage that failed with timeout.
-
delayed
<A> CompletionStage<A> delayed(Callable<CompletionStage<A>> callable, long amount, TimeUnit unit)
Create aCompletionStage
which, after a delay, will be redeemed with the result of a given callable. The completion stage will be called after the delay.- Type Parameters:
A
- the type of the completion's result.- Parameters:
callable
- the input completion stage that is called after the delay.amount
- The time to wait.unit
- The units to use for the amount.- Returns:
- the delayed CompletionStage wrapping supplier.
-
delay
CompletionStage<Done> delay(Duration duration)
Creates a completion stage which is only completed after the delay.Duration expected = Duration.ofSeconds(2); long start = System.currentTimeMillis(); CompletionStage<Long> stage = futures.delay(expected).thenApply((v) -> { long end = System.currentTimeMillis(); return (end - start); });
- Parameters:
duration
- the duration after which the completion stage is run.- Returns:
- the completion stage.
-
delay
CompletionStage<Done> delay(long amount, TimeUnit unit)
Creates a completion stage which is only completed after the delay.- Parameters:
amount
- The time to wait.unit
- The units to use for the amount.- Returns:
- the delayed CompletionStage.
-
delayed
<A> CompletionStage<A> delayed(Callable<CompletionStage<A>> callable, Duration duration)
Create aCompletionStage
which, after a delay, will be redeemed with the result of a given supplier. The completion stage will be called after the delay.For example, to render a number indicating the delay, you can use the following method:
private CompletionStage<Long> renderAfter(Duration duration) { long start = System.currentTimeMillis(); return futures.delayed(() -> { long end = System.currentTimeMillis(); return CompletableFuture.completedFuture(end - start); }, duration); }
- Type Parameters:
A
- the type of the completion's result.- Parameters:
callable
- the input completion stage that is called after the delay.duration
- to wait.- Returns:
- the delayed CompletionStage wrapping supplier.
-
sequence
static <A> CompletionStage<List<A>> sequence(Iterable<? extends CompletionStage<A>> promises)
Combine the given CompletionStages into a singleCompletionStage
for the list of results.The sequencing operations are performed in the default ExecutionContext.
- Type Parameters:
A
- the type of the completion's result.- Parameters:
promises
- The CompletionStages to combine- Returns:
- A single CompletionStage whose methods act on the list of redeemed CompletionStages
-
sequence
@SafeVarargs static <A> CompletionStage<List<A>> sequence(CompletionStage<A>... promises)
Combine the given CompletionStages into a single CompletionStage for the list of results.The sequencing operations are performed in the default ExecutionContext.
- Type Parameters:
A
- the type of the completion's result.- Parameters:
promises
- The CompletionStages to combine- Returns:
- A single CompletionStage whose methods act on the list of redeemed CompletionStage
-
-