All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.swirlds.common.threading.futures.FuturePool Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 0.56.6
Show newest version
/*
 * 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.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Consumer;

public class FuturePool extends LinkedList> {

    private final Consumer exceptionHandler;

    private static volatile boolean shuttingDown = false;

    static {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            shuttingDown = true;
        }));
    }

    /**
     * Constructs an empty list.
     */
    public FuturePool() {
        exceptionHandler = null;
    }

    /**
     * Constructs an empty list.
     *
     * @param exceptionHandler
     * 		an handler which handles exceptions thrown during the computation
     */
    public FuturePool(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
     * @param exceptionHandler
     * 		an handler which handles exceptions thrown during the computation
     * @throws NullPointerException
     * 		if the specified collection is null
     */
    public FuturePool(final Collection> c, final Consumer exceptionHandler) {
        super(c);
        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 FuturePool(final Collection> c) {
        super(c);
        this.exceptionHandler = null;
    }

    /**
     * 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy