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   * @see SchedulingStrategy
35   * @see SharedThreadPoolStrategy
36   * @see NonSharedThreadPoolStrategy
37   * @since 2.16
38   */
39  abstract class AbstractThreadPoolStrategy
40      extends SchedulingStrategy
41  {
42      private final ExecutorService threadPool;
43  
44      private final Collection<Future<?>> futureResults;
45  
46      private final AtomicBoolean canSchedule = new AtomicBoolean( true );
47  
48      AbstractThreadPoolStrategy( ExecutorService threadPool )
49      {
50          this( threadPool, null );
51      }
52  
53      AbstractThreadPoolStrategy( ExecutorService threadPool, Collection<Future<?>> futureResults )
54      {
55          this.threadPool = threadPool;
56          this.futureResults = futureResults;
57      }
58  
59      protected final ExecutorService getThreadPool()
60      {
61          return threadPool;
62      }
63  
64      protected final Collection<Future<?>> getFutureResults()
65      {
66          return futureResults;
67      }
68  
69      protected final void disable()
70      {
71          canSchedule.set( false );
72      }
73  
74      @Override
75      public void schedule( Runnable task )
76      {
77          if ( canSchedule() )
78          {
79              Future<?> futureResult = threadPool.submit( task );
80              if ( futureResults != null )
81              {
82                  futureResults.add( futureResult );
83              }
84          }
85      }
86  
87      @Override
88      protected boolean stop()
89      {
90          boolean wasRunning = canSchedule.getAndSet( false );
91          if ( threadPool.isShutdown() )
92          {
93              wasRunning = false;
94          }
95          else
96          {
97              threadPool.shutdown();
98          }
99          return wasRunning;
100     }
101 
102     @Override
103     protected boolean stopNow()
104     {
105         boolean wasRunning = canSchedule.getAndSet( false );
106         if ( threadPool.isShutdown() )
107         {
108             wasRunning = false;
109         }
110         else
111         {
112             threadPool.shutdownNow();
113         }
114         return wasRunning;
115     }
116 
117     @Override
118     protected void setDefaultShutdownHandler( Scheduler.ShutdownHandler handler )
119     {
120         if ( threadPool instanceof ThreadPoolExecutor )
121         {
122             ThreadPoolExecutor pool = (ThreadPoolExecutor) threadPool;
123             handler.setRejectedExecutionHandler( pool.getRejectedExecutionHandler() );
124             pool.setRejectedExecutionHandler( handler );
125         }
126     }
127 
128     @Override
129     public final boolean canSchedule()
130     {
131         return canSchedule.get();
132     }
133 }