1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.util.concurrency;
20
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.LinkedBlockingQueue;
24 import java.util.concurrent.ThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26
27 import org.eclipse.aether.RepositorySystemSession;
28 import org.eclipse.aether.util.ConfigUtils;
29
30 /**
31 * Utilities for executors and sizing them.
32 *
33 * @since 1.9.5
34 * @deprecated for removal. Nothing is using this class within Resolver.
35 */
36 @Deprecated
37 public final class ExecutorUtils {
38 /**
39 * Shared instance of "direct executor".
40 */
41 public static final Executor DIRECT_EXECUTOR = Runnable::run;
42
43 /**
44 * Creates new thread pool {@link ExecutorService}. The {@code poolSize} parameter but be greater than 1.
45 */
46 public static ExecutorService threadPool(int poolSize, String namePrefix) {
47 if (poolSize < 2) {
48 throw new IllegalArgumentException("Invalid poolSize: " + poolSize + ". Must be greater than 1.");
49 }
50 return new ThreadPoolExecutor(
51 poolSize,
52 poolSize,
53 3L,
54 TimeUnit.SECONDS,
55 new LinkedBlockingQueue<>(),
56 new WorkerThreadFactory(namePrefix));
57 }
58
59 /**
60 * Returns {@link #DIRECT_EXECUTOR} or result of {@link #threadPool(int, String)} depending on value of
61 * {@code size} parameter.
62 */
63 public static Executor executor(int size, String namePrefix) {
64 if (size <= 1) {
65 return DIRECT_EXECUTOR;
66 } else {
67 return threadPool(size, namePrefix);
68 }
69 }
70
71 /**
72 * To be used with result of {@link #executor(int, String)} method, shuts down instance if it is
73 * {@link ExecutorService}.
74 */
75 public static void shutdown(Executor executor) {
76 if (executor instanceof ExecutorService) {
77 ((ExecutorService) executor).shutdown();
78 }
79 }
80
81 /**
82 * Retrieves and validates requested thread count based on session and specified keys, or if none provided, the
83 * provided default value. This method validates result on top of what {@link ConfigUtils} does.
84 *
85 * @throws IllegalArgumentException if default value is less than 1
86 * @see ConfigUtils#getInteger(RepositorySystemSession, int, String...)
87 */
88 public static int threadCount(RepositorySystemSession session, int defaultValue, String... keys) {
89 if (defaultValue < 1) {
90 throw new IllegalArgumentException("Invalid defaultValue: " + defaultValue + ". Must be greater than 0.");
91 }
92 int threadCount = ConfigUtils.getInteger(session, defaultValue, keys);
93 if (threadCount < 1) {
94 throw new IllegalArgumentException("Invalid value: " + threadCount + ". Must be greater than 0.");
95 }
96 return threadCount;
97 }
98 }