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.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   * Abstract parallel scheduling strategy in private package.
30   * The remaining abstract methods have to be implemented differently
31   * depending if the thread pool is shared with other strategies or not.
32   *
33   * @author Tibor Digana (tibor17)
34   * @since 2.16
35   *
36   * @see SchedulingStrategy
37   * @see SharedThreadPoolStrategy
38   * @see NonSharedThreadPoolStrategy
39   */
40  abstract class AbstractThreadPoolStrategy extends SchedulingStrategy {
41      private final ExecutorService threadPool;
42      private final Collection<Future<?>> futureResults;
43      private final AtomicBoolean canSchedule = new AtomicBoolean(true);
44  
45      AbstractThreadPoolStrategy(ExecutorService threadPool) {
46          this(threadPool, null);
47      }
48  
49      AbstractThreadPoolStrategy(ExecutorService threadPool, Collection<Future<?>> futureResults) {
50          this.threadPool = threadPool;
51          this.futureResults = futureResults;
52      }
53  
54      protected final ExecutorService getThreadPool() {
55          return threadPool;
56      }
57  
58      protected final Collection<Future<?>> getFutureResults() {
59          return futureResults;
60      }
61  
62      protected final void disable() {
63          canSchedule.set(false);
64      }
65  
66      @Override
67      public void schedule(Runnable task) {
68          if (canSchedule()) {
69              Future<?> futureResult = threadPool.submit(task);
70              if (futureResults != null) {
71                  futureResults.add(futureResult);
72              }
73          }
74      }
75  
76      @Override
77      protected boolean stop() {
78          boolean wasRunning = canSchedule.getAndSet(false);
79          if (threadPool.isShutdown()) {
80              wasRunning = false;
81          } else {
82              threadPool.shutdown();
83          }
84          return wasRunning;
85      }
86  
87      @Override
88      protected boolean stopNow() {
89          boolean wasRunning = canSchedule.getAndSet(false);
90          if (threadPool.isShutdown()) {
91              wasRunning = false;
92          } else {
93              threadPool.shutdownNow();
94          }
95          return wasRunning;
96      }
97  
98      @Override
99      protected void setDefaultShutdownHandler(Scheduler.ShutdownHandler handler) {
100         if (threadPool instanceof ThreadPoolExecutor) {
101             ThreadPoolExecutor pool = (ThreadPoolExecutor) threadPool;
102             handler.setRejectedExecutionHandler(pool.getRejectedExecutionHandler());
103             pool.setRejectedExecutionHandler(handler);
104         }
105     }
106 
107     @Override
108     public final boolean canSchedule() {
109         return canSchedule.get();
110     }
111 }