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

CompletableFutureexceptionallyCompletedFuture(final Throwable e)
Creates a CompletableFuture which is completed exceptionally with the given Exception.
return failed(e);
CompletionStageexceptionallyCompletedFuture(final Throwable t)
exceptionally Completed Future
CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(t);
return future;
voidgetAll(List> futures)
get All
for (final Future future : futures) {
    try {
        future.get();
    } catch (ExecutionException | InterruptedException e) {
        throw new RuntimeException(e);
ListgetAll(List> futures)
get All
try {
    List<T> result = new ArrayList<T>(futures.size());
    for (Future<T> future : futures) {
        result.add(future.get());
    return result;
} catch (Exception e) {
    throw new RuntimeException("Exception while getting result of multiple futures", e);
...
ListgetAllDone(Collection futures)
Get all futures that are done
List<Future> doneFutures = new ArrayList<Future>();
for (Future f : futures) {
    if (f.isDone()) {
        doneFutures.add(f);
return doneFutures;
ThrowablegetExceptionFromNewThread(String threadName, Runnable target)
get Exception From New Thread
try {
    runInNewThread(threadName, target);
} catch (Throwable throwable) {
    return throwable;
throw new AssertionError("Expected to throw an exception but did not throw anything");
CompletableFuturegetFailedFuture(Throwable throwable)
Returns a CompletableFuture that has failed with the exception provided as argument.
CompletableFuture<T> failedAttempt = new CompletableFuture<>();
failedAttempt.completeExceptionally(throwable);
return failedAttempt;
TgetFuture(Future future, String message)
Retrieves the value of a Future and throws AssertionError on failures.
try {
    return future.get();
} catch (InterruptedException e) {
    throw new AssertionError(message + " " + e.getMessage());
} catch (ExecutionException e) {
    throw new AssertionError(message + " " + e.getMessage());
longgetMinIn(Map> responses)
get Min In
Iterable<Long> collection = Iterables.transform(responses.values(), new Function<Future<Long>, Long>() {
    @Override
    public Long apply(Future<Long> from) {
        try {
            return from.get();
        } catch (InterruptedException e) {
        } catch (ExecutionException e) {
        return null;
});
long time = Collections.min(Sets.newHashSet(collection));
return time;
ListgetResults(Iterable> futures)
Checks results for exceptions by calling all Future#get() .
List<T> results = new ArrayList<>();
for (Future<T> future : futures) {
    try {
        results.add(future.get());
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    } catch (ExecutionException e) {
        throw new IllegalStateException(e.getCause());
...