001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.concurrency;
020
021import java.util.concurrent.Executor;
022import java.util.concurrent.ExecutorService;
023import java.util.concurrent.LinkedBlockingQueue;
024import java.util.concurrent.ThreadPoolExecutor;
025import java.util.concurrent.TimeUnit;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.util.ConfigUtils;
029
030/**
031 * Utilities for executors and sizing them.
032 *
033 * @since 1.9.5
034 * @deprecated For removal. Nothing is using this class within Resolver.
035 */
036@Deprecated
037public final class ExecutorUtils {
038    /**
039     * Shared instance of "direct executor".
040     */
041    public static final Executor DIRECT_EXECUTOR = Runnable::run;
042
043    /**
044     * Creates new thread pool {@link ExecutorService}. The {@code poolSize} parameter but be greater than 1.
045     */
046    public static ExecutorService threadPool(int poolSize, String namePrefix) {
047        if (poolSize < 2) {
048            throw new IllegalArgumentException("Invalid poolSize: " + poolSize + ". Must be greater than 1.");
049        }
050        return new ThreadPoolExecutor(
051                poolSize,
052                poolSize,
053                3L,
054                TimeUnit.SECONDS,
055                new LinkedBlockingQueue<>(),
056                new WorkerThreadFactory(namePrefix));
057    }
058
059    /**
060     * Returns {@link #DIRECT_EXECUTOR} or result of {@link #threadPool(int, String)} depending on value of
061     * {@code size} parameter.
062     */
063    public static Executor executor(int size, String namePrefix) {
064        if (size <= 1) {
065            return DIRECT_EXECUTOR;
066        } else {
067            return threadPool(size, namePrefix);
068        }
069    }
070
071    /**
072     * To be used with result of {@link #executor(int, String)} method, shuts down instance if it is
073     * {@link ExecutorService}.
074     */
075    public static void shutdown(Executor executor) {
076        if (executor instanceof ExecutorService) {
077            ((ExecutorService) executor).shutdown();
078        }
079    }
080
081    /**
082     * Retrieves and validates requested thread count based on session and specified keys, or if none provided, the
083     * provided default value. This method validates result on top of what {@link ConfigUtils} does.
084     *
085     * @throws IllegalArgumentException if default value is less than 1.
086     * @see ConfigUtils#getInteger(RepositorySystemSession, int, String...)
087     */
088    public static int threadCount(RepositorySystemSession session, int defaultValue, String... keys) {
089        if (defaultValue < 1) {
090            throw new IllegalArgumentException("Invalid defaultValue: " + defaultValue + ". Must be greater than 0.");
091        }
092        int threadCount = ConfigUtils.getInteger(session, defaultValue, keys);
093        if (threadCount < 1) {
094            throw new IllegalArgumentException("Invalid value: " + threadCount + ". Must be greater than 0.");
095        }
096        return threadCount;
097    }
098}