View Javadoc
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      /**
35       * @return sequentially executing strategy
36       */
37      public static SchedulingStrategy createInvokerStrategy()
38      {
39          return new InvokerStrategy();
40      }
41  
42      /**
43       * @param nThreads fixed pool capacity
44       * @return parallel scheduling strategy
45       */
46      public static SchedulingStrategy createParallelStrategy( int nThreads )
47      {
48          return new NonSharedThreadPoolStrategy( Executors.newFixedThreadPool( nThreads ) );
49      }
50  
51      /**
52       * @return parallel scheduling strategy with unbounded capacity
53       */
54      public static SchedulingStrategy createParallelStrategyUnbounded()
55      {
56          return new NonSharedThreadPoolStrategy( Executors.newCachedThreadPool() );
57      }
58  
59      /**
60       * The <tt>threadPool</tt> passed to this strategy can be shared in other strategies.
61       * <p/>
62       * The call {@link SchedulingStrategy#finished()} is waiting until own tasks have finished.
63       * New tasks will not be scheduled by this call in this strategy. This strategy is not
64       * waiting for other strategies to finish. The {@link org.junit.runners.model.RunnerScheduler#finished()} may
65       * freely use {@link SchedulingStrategy#finished()}.
66       *
67       * @param threadPool thread pool possibly shared with other strategies
68       * @return parallel strategy with shared thread pool
69       * @throws NullPointerException if <tt>threadPool</tt> is null
70       */
71      public static SchedulingStrategy createParallelSharedStrategy( ExecutorService threadPool )
72      {
73          if ( threadPool == null )
74          {
75              throw new NullPointerException( "null threadPool in #createParallelSharedStrategy" );
76          }
77          return new SharedThreadPoolStrategy( threadPool );
78      }
79  }