1 | |
package org.apache.maven.surefire.junitcore.pc; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.Collection; |
23 | |
import java.util.concurrent.ExecutorService; |
24 | |
import java.util.concurrent.Future; |
25 | |
import java.util.concurrent.ThreadPoolExecutor; |
26 | |
import java.util.concurrent.atomic.AtomicBoolean; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
abstract class AbstractThreadPoolStrategy extends SchedulingStrategy { |
41 | |
private final ExecutorService threadPool; |
42 | |
private final Collection<Future<?>> futureResults; |
43 | 102 | private final AtomicBoolean canSchedule = new AtomicBoolean(true); |
44 | |
|
45 | |
AbstractThreadPoolStrategy(ExecutorService threadPool) { |
46 | 15 | this(threadPool, null); |
47 | 15 | } |
48 | |
|
49 | 102 | AbstractThreadPoolStrategy(ExecutorService threadPool, Collection<Future<?>> futureResults) { |
50 | 102 | this.threadPool = threadPool; |
51 | 102 | this.futureResults = futureResults; |
52 | 102 | } |
53 | |
|
54 | |
protected final ExecutorService getThreadPool() { |
55 | 36 | return threadPool; |
56 | |
} |
57 | |
|
58 | |
protected final Collection<Future<?>> getFutureResults() { |
59 | 185 | return futureResults; |
60 | |
} |
61 | |
|
62 | |
protected final void disable() { |
63 | 203 | canSchedule.set(false); |
64 | 203 | } |
65 | |
|
66 | |
@Override |
67 | |
public void schedule(Runnable task) { |
68 | 184 | if (canSchedule()) { |
69 | 181 | Future<?> futureResult = threadPool.submit(task); |
70 | 181 | if (futureResults != null) { |
71 | 154 | futureResults.add(futureResult); |
72 | |
} |
73 | |
} |
74 | 184 | } |
75 | |
|
76 | |
@Override |
77 | |
protected boolean stop() { |
78 | 1 | boolean wasRunning = canSchedule.getAndSet(false); |
79 | 1 | if (threadPool.isShutdown()) { |
80 | 1 | wasRunning = false; |
81 | |
} else { |
82 | 0 | threadPool.shutdown(); |
83 | |
} |
84 | 1 | return wasRunning; |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
protected boolean stopNow() { |
89 | 1 | boolean wasRunning = canSchedule.getAndSet(false); |
90 | 1 | if (threadPool.isShutdown()) { |
91 | 1 | wasRunning = false; |
92 | |
} else { |
93 | 0 | threadPool.shutdownNow(); |
94 | |
} |
95 | 1 | return wasRunning; |
96 | |
} |
97 | |
|
98 | |
@Override |
99 | |
protected void setDefaultShutdownHandler(Scheduler.ShutdownHandler handler) { |
100 | 186 | if (threadPool instanceof ThreadPoolExecutor) { |
101 | 186 | ThreadPoolExecutor pool = (ThreadPoolExecutor) threadPool; |
102 | 186 | handler.setRejectedExecutionHandler(pool.getRejectedExecutionHandler()); |
103 | 186 | pool.setRejectedExecutionHandler(handler); |
104 | |
} |
105 | 186 | } |
106 | |
|
107 | |
@Override |
108 | |
public final boolean canSchedule() { |
109 | 575 | return canSchedule.get(); |
110 | |
} |
111 | |
} |