1 package org.apache.maven.surefire.junitcore.pc;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Executors;
24
25 /**
26 * The factory of {@link SchedulingStrategy}.
27 *
28 * @author Tibor Digana (tibor17)
29 * @since 2.16
30 */
31 public class SchedulingStrategies {
32
33 /**
34 * @return sequentially executing strategy
35 */
36 public static SchedulingStrategy createInvokerStrategy() {
37 return new InvokerStrategy();
38 }
39
40 /**
41 * @param nThreads fixed pool capacity
42 * @return parallel scheduling strategy
43 */
44 public static SchedulingStrategy createParallelStrategy(int nThreads) {
45 return new NonSharedThreadPoolStrategy(Executors.newFixedThreadPool(nThreads));
46 }
47
48 /**
49 * @return parallel scheduling strategy with unbounded capacity
50 */
51 public static SchedulingStrategy createParallelStrategyUnbounded() {
52 return new NonSharedThreadPoolStrategy(Executors.newCachedThreadPool());
53 }
54
55 /**
56 * The <tt>threadPool</tt> passed to this strategy can be shared in other strategies.
57 * <p>
58 * The call {@link SchedulingStrategy#finished()} is waiting until own tasks have finished.
59 * New tasks will not be scheduled by this call in this strategy. This strategy is not
60 * waiting for other strategies to finish. The {@link org.junit.runners.model.RunnerScheduler#finished()} may
61 * freely use {@link SchedulingStrategy#finished()}.
62 *
63 * @param threadPool thread pool possibly shared with other strategies
64 * @return parallel strategy with shared thread pool
65 * @throws NullPointerException if <tt>threadPool</tt> is null
66 */
67 public static SchedulingStrategy createParallelSharedStrategy(ExecutorService threadPool) {
68 if (threadPool == null) {
69 throw new NullPointerException("null threadPool in #createParallelSharedStrategy");
70 }
71 return new SharedThreadPoolStrategy(threadPool);
72 }
73 }