19
19
import java .util .Deque ;
20
20
import java .util .LinkedList ;
21
21
import java .util .List ;
22
+ import java .util .Optional ;
23
+ import java .util .concurrent .Callable ;
22
24
import java .util .concurrent .ForkJoinPool ;
23
25
import java .util .concurrent .ForkJoinPool .ForkJoinWorkerThreadFactory ;
24
26
import java .util .concurrent .ForkJoinTask ;
25
27
import java .util .concurrent .ForkJoinWorkerThread ;
26
28
import java .util .concurrent .Future ;
27
29
import java .util .concurrent .RecursiveAction ;
28
30
import java .util .concurrent .TimeUnit ;
31
+ import java .util .function .Function ;
29
32
import java .util .function .Predicate ;
30
33
31
34
import org .apiguardian .api .API ;
@@ -81,18 +84,32 @@ private static ParallelExecutionConfiguration createConfiguration(ConfigurationP
81
84
82
85
private ForkJoinPool createForkJoinPool (ParallelExecutionConfiguration configuration ) {
83
86
ForkJoinWorkerThreadFactory threadFactory = new WorkerThreadFactory ();
84
- return Try .call (() -> {
85
- // Try to use constructor available in Java >= 9
86
- Constructor <ForkJoinPool > constructor = ForkJoinPool .class .getDeclaredConstructor (Integer .TYPE ,
87
- ForkJoinWorkerThreadFactory .class , UncaughtExceptionHandler .class , Boolean .TYPE , Integer .TYPE ,
88
- Integer .TYPE , Integer .TYPE , Predicate .class , Long .TYPE , TimeUnit .class );
89
- return constructor .newInstance (configuration .getParallelism (), threadFactory , null , false ,
90
- configuration .getCorePoolSize (), configuration .getMaxPoolSize (), configuration .getMinimumRunnable (),
91
- configuration .getSaturatePredicate (), configuration .getKeepAliveSeconds (), TimeUnit .SECONDS );
92
- }).orElseTry (() -> {
93
- // Fallback for Java 8
94
- return new ForkJoinPool (configuration .getParallelism (), threadFactory , null , false );
95
- }).getOrThrow (cause -> new JUnitException ("Failed to create ForkJoinPool" , cause ));
87
+ // Try to use constructor available in Java >= 9
88
+ Callable <ForkJoinPool > constructorInvocation = sinceJava9Constructor () //
89
+ .map (sinceJava9ConstructorInvocation (configuration , threadFactory ))
90
+ // Fallback for Java 8
91
+ .orElse (sinceJava7ConstructorInvocation (configuration , threadFactory ));
92
+ return Try .call (constructorInvocation ) //
93
+ .getOrThrow (cause -> new JUnitException ("Failed to create ForkJoinPool" , cause ));
94
+ }
95
+
96
+ private static Optional <Constructor <ForkJoinPool >> sinceJava9Constructor () {
97
+ return Try .call (() -> ForkJoinPool .class .getDeclaredConstructor (Integer .TYPE , ForkJoinWorkerThreadFactory .class ,
98
+ UncaughtExceptionHandler .class , Boolean .TYPE , Integer .TYPE , Integer .TYPE , Integer .TYPE , Predicate .class ,
99
+ Long .TYPE , TimeUnit .class )) //
100
+ .toOptional ();
101
+ }
102
+
103
+ private static Function <Constructor <ForkJoinPool >, Callable <ForkJoinPool >> sinceJava9ConstructorInvocation (
104
+ ParallelExecutionConfiguration configuration , ForkJoinWorkerThreadFactory threadFactory ) {
105
+ return constructor -> () -> constructor .newInstance (configuration .getParallelism (), threadFactory , null , false ,
106
+ configuration .getCorePoolSize (), configuration .getMaxPoolSize (), configuration .getMinimumRunnable (),
107
+ configuration .getSaturatePredicate (), configuration .getKeepAliveSeconds (), TimeUnit .SECONDS );
108
+ }
109
+
110
+ private static Callable <ForkJoinPool > sinceJava7ConstructorInvocation (ParallelExecutionConfiguration configuration ,
111
+ ForkJoinWorkerThreadFactory threadFactory ) {
112
+ return () -> new ForkJoinPool (configuration .getParallelism (), threadFactory , null , false );
96
113
}
97
114
98
115
@ Override
@@ -199,6 +216,7 @@ static class WorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFac
199
216
public ForkJoinWorkerThread newThread (ForkJoinPool pool ) {
200
217
return new WorkerThread (pool , contextClassLoader );
201
218
}
219
+
202
220
}
203
221
204
222
static class WorkerThread extends ForkJoinWorkerThread {
@@ -207,6 +225,7 @@ static class WorkerThread extends ForkJoinWorkerThread {
207
225
super (pool );
208
226
setContextClassLoader (contextClassLoader );
209
227
}
228
+
210
229
}
211
230
212
231
}
0 commit comments