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 public final class ExecutorUtils {
36
37
38
39 public static final Executor DIRECT_EXECUTOR = Runnable::run;
40
41
42
43
44 public static ExecutorService threadPool(int poolSize, String namePrefix) {
45 if (poolSize < 2) {
46 throw new IllegalArgumentException("Invalid poolSize: " + poolSize + ". Must be greater than 1.");
47 }
48 return new ThreadPoolExecutor(
49 poolSize,
50 poolSize,
51 3L,
52 TimeUnit.SECONDS,
53 new LinkedBlockingQueue<>(),
54 new WorkerThreadFactory(namePrefix));
55 }
56
57
58
59
60
61 public static Executor executor(int size, String namePrefix) {
62 if (size <= 1) {
63 return DIRECT_EXECUTOR;
64 } else {
65 return threadPool(size, namePrefix);
66 }
67 }
68
69
70
71
72
73 public static void shutdown(Executor executor) {
74 if (executor instanceof ExecutorService) {
75 ((ExecutorService) executor).shutdown();
76 }
77 }
78
79
80
81
82
83
84
85
86 public static int threadCount(RepositorySystemSession session, int defaultValue, String... keys) {
87 if (defaultValue < 1) {
88 throw new IllegalArgumentException("Invalid defaultValue: " + defaultValue + ". Must be greater than 0.");
89 }
90 int threadCount = ConfigUtils.getInteger(session, defaultValue, keys);
91 if (threadCount < 1) {
92 throw new IllegalArgumentException("Invalid value: " + threadCount + ". Must be greater than 0.");
93 }
94 return threadCount;
95 }
96 }