Java Utililty Methods Thread Future

List of utility methods to do Thread Future

Description

The list of methods to do Thread Future are organized into topic(s).

Method

CompletableFuture>sequence(List> futures)
sequence
CompletableFuture<Void> allDoneFuture = CompletableFuture
        .allOf(futures.toArray(new CompletableFuture[futures.size()]));
return allDoneFuture
        .thenApply(v -> futures.stream().map(CompletableFuture::join).collect(Collectors.<T>toList()));
CompletableFuture>sequence(List> futures)
sequence
CompletableFuture<Void> allDoneFuture = CompletableFuture
        .allOf(futures.toArray(new CompletableFuture[futures.size()]));
return allDoneFuture
        .thenApply(v -> futures.stream().map(future -> future.join()).collect(Collectors.<T>toList()));
CompletableFuture>sequence(List> futures)
sequence
if (futures.isEmpty()) {
    return CompletableFuture.completedFuture(Collections.emptyList());
CompletableFuture[] fa = futures.toArray(new CompletableFuture[futures.size()]);
return CompletableFuture.allOf(fa).thenApply(v -> {
    List<T> results = new ArrayList<>(futures.size());
    for (CompletableFuture<T> cf : futures) {
        results.add(cf.join());
...
CompletableFuture>sequence(List> futures)
sequence
return CompletableFuture
        .supplyAsync(() -> futures.stream().map(future -> future.join()).collect(Collectors.<T>toList()));
List>submitTasks(ExecutorCompletionService ecs, Iterable> tasks)
submit Tasks
List<Future<V>> futures = new LinkedList<Future<V>>();
if (tasks != null) {
    for (Callable<V> callable : tasks) {
        futures.add(ecs.submit(callable));
return futures;
longsumFutures(List> futures)
Given futures from the various ThreadServiceStats, wait for them to finish and add their numeric values together.
long total = 0;
for (Future<Object> f : futures) {
    try {
        total += (Long) f.get();
    } catch (Exception ignore) {
        System.out.println("### sumFutures got exception!");
return total;
voidtransferResult(final CompletionStage source, final CompletableFuture target)
transfer Result
source.whenComplete((result, throwable) -> {
    final boolean isSuccessful = throwable == null;
    if (isSuccessful) {
        target.complete(result);
    } else {
        target.completeExceptionally(throwable);
});
...
voidtransferResult(final CompletionStage source, final CompletableFuture target)
Internal JVM SDK util.
source.whenCompleteAsync((result, throwable) -> {
    final boolean isSuccessful = throwable == null;
    if (isSuccessful) {
        target.complete(result);
    } else {
        target.completeExceptionally(throwable);
});
...
voidunwrap(Throwable throwable, Class throwableA)
Unwraps ExecutionException thrown from a CompletableFuture.
Throwable unwrapThrowable = throwable;
if (throwable instanceof ExecutionException || throwable instanceof CompletionException) {
    unwrapThrowable = throwable.getCause();
if (throwableA.isInstance(unwrapThrowable)) {
    throw (A) unwrapThrowable;
if (unwrapThrowable instanceof RuntimeException) {
...
TwaitFor(Future future)
wait For
boolean interrupted = false;
try {
    while (true) {
        try {
            return future.get();
        } catch (InterruptedException e) {
            interrupted = true;
} finally {
    if (interrupted) {
        Thread.currentThread().interrupt();