1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
35
36 @Deprecated
37 public final class ExecutorUtils {
38
39
40
41 public static final Executor DIRECT_EXECUTOR = Runnable::run;
42
43
44
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
61
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
73
74
75 public static void shutdown(Executor executor) {
76 if (executor instanceof ExecutorService) {
77 ((ExecutorService) executor).shutdown();
78 }
79 }
80
81
82
83
84
85
86
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 }