com.swirlds.common.threading.futures.ConcurrentFuturePool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swirlds-common Show documentation
Show all versions of swirlds-common Show documentation
Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.
/*
* Copyright (C) 2018-2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.swirlds.common.threading.futures;
import com.swirlds.common.exceptions.PlatformException;
import com.swirlds.logging.legacy.LogMarker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;
public class ConcurrentFuturePool extends ConcurrentLinkedQueue> {
private final Consumer exceptionHandler;
private static volatile boolean shuttingDown = false;
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shuttingDown = true;
}));
}
/**
* Constructs an empty list.
*/
public ConcurrentFuturePool() {
exceptionHandler = null;
}
/**
* Constructs an empty list.
*
* @param exceptionHandler
* an handler which handles exceptions thrown during the computation
*/
public ConcurrentFuturePool(final Consumer exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
/**
* Constructs a list containing the elements of the specified collection, in the order they are returned by the
* collection's iterator.
*
* @param c
* the collection whose elements are to be placed into this list
* @throws NullPointerException
* if the specified collection is null
*/
public ConcurrentFuturePool(final Collection extends Future> c) {
super(c);
this.exceptionHandler = null;
}
/**
* Constructs a list containing the elements of the specified collection, in the order they are returned by the
* collection's iterator.
*
* @param c
* the collection whose elements are to be placed into this list
* @param exceptionHandler
* an handler which handles exceptions thrown during the computation
* @throws NullPointerException
* if the specified collection is null
*/
public ConcurrentFuturePool(final Collection extends Future> c, final Consumer exceptionHandler) {
super(c);
this.exceptionHandler = exceptionHandler;
}
/**
* Constructs a list containing the elements of the specified {@link SortedSet}, in the order they are returned by
* the collection's iterator.
*
* @param s
* the {@link SortedSet} whose elements are to be placed into this list
* @throws NullPointerException
* if the specified collection is null
*/
public ConcurrentFuturePool(final SortedSet> s) {
super(s);
this.exceptionHandler = null;
}
/**
* Constructs a list containing the elements of the specified {@link SortedSet}, in the order they are returned by
* the collection's iterator.
*
* @param s
* the {@link SortedSet} whose elements are to be placed into this list
* @param exceptionHandler
* an handler which handles exceptions thrown during the computation
* @throws NullPointerException
* if the specified collection is null
*/
public ConcurrentFuturePool(final SortedSet> s, final Consumer exceptionHandler) {
super(s);
this.exceptionHandler = exceptionHandler;
}
/**
* Determines if all futures have been completed or cancelled.
*
* @return true if all futures are completed or cancelled, otherwise false
*/
public boolean isComplete() {
removeIf((v) -> v.isDone() || v.isCancelled());
return size() == 0;
}
/**
* Getter that returns true if the shutdown hook has been called by the JVM.
*
* @return true if the JVM is shutting down; false otherwise
*/
private static boolean isShuttingDown() {
return shuttingDown;
}
/**
* Waits (indefinitely) for all futures to either complete or be cancelled.
*
* @return an ordered {@link List} containing all values (or {@code null} if cancelled) returned by the futures
* contained in this {@link FuturePool}.
*/
public List waitForCompletion() {
final List results = new ArrayList<>(size());
forEach((f) -> {
if (f.isCancelled()) {
results.add(null);
return;
}
try {
results.add(f.get());
} catch (InterruptedException | ExecutionException ex) {
if (exceptionHandler != null) {
if (!isShuttingDown()) {
exceptionHandler.accept(ex);
}
} else {
if (!isShuttingDown()) {
if (ex instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new PlatformException(ex, LogMarker.EXCEPTION);
}
}
}
});
return results;
}
}