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