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

alpine.common.util.ExecutorUtil Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package alpine.common.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Utility methods for working with {@link ExecutorService}s.
 *
 * @since 2.3.0
 */
public final class ExecutorUtil {

    public record ExecutorStats(boolean terminated, Integer queueSize, Integer activeThreads) {
    }

    private ExecutorUtil() {
    }

    /**
     * Gathers {@link ExecutorStats} about a given {@link ExecutorService}.
     *
     * @param executor The {@link ExecutorService} to collect stats for
     * @return The collected {@link ExecutorStats}
     */
    public static ExecutorStats getExecutorStats(final ExecutorService executor) {
        if (executor instanceof final ThreadPoolExecutor tpExecutor) {
            return new ExecutorStats(tpExecutor.isTerminated(), tpExecutor.getQueue().size(), tpExecutor.getActiveCount());
        } else if (executor instanceof final ForkJoinPool fjpExecutor) {
            return new ExecutorStats(fjpExecutor.isTerminated(), fjpExecutor.getQueuedSubmissionCount(), fjpExecutor.getActiveThreadCount());
        }

        return new ExecutorStats(executor.isTerminated(), null, null);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy