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.TimeUnit;
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 volatile boolean isDestroyed;
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      @Override
70      public void schedule( Runnable task )
71      {
72          if ( canSchedule() )
73          {
74              Future<?> futureResult = threadPool.submit( task );
75              if ( futureResults != null )
76              {
77                  futureResults.add( futureResult );
78              }
79          }
80      }
81  
82      @Override
83      protected boolean stop()
84      {
85          boolean wasRunning = disable();
86          if ( threadPool.isShutdown() )
87          {
88              wasRunning = false;
89          }
90          else
91          {
92              threadPool.shutdown();
93          }
94          return wasRunning;
95      }
96  
97      @Override
98      protected boolean stopNow()
99      {
100         boolean wasRunning = disable();
101         if ( threadPool.isShutdown() )
102         {
103             wasRunning = false;
104         }
105         else
106         {
107             threadPool.shutdownNow();
108         }
109         return wasRunning;
110     }
111 
112     /**
113      * @see Scheduler.ShutdownHandler
114      */
115     @Override
116     protected void setDefaultShutdownHandler( Scheduler.ShutdownHandler handler )
117     {
118         if ( threadPool instanceof ThreadPoolExecutor )
119         {
120             ThreadPoolExecutor pool = (ThreadPoolExecutor) threadPool;
121             handler.setRejectedExecutionHandler( pool.getRejectedExecutionHandler() );
122             pool.setRejectedExecutionHandler( handler );
123         }
124     }
125 
126     public boolean destroy()
127     {
128         try
129         {
130             if ( !isDestroyed )//just an optimization
131             {
132                 disable();
133                 threadPool.shutdown();
134                 this.isDestroyed |= threadPool.awaitTermination( Long.MAX_VALUE, TimeUnit.NANOSECONDS );
135             }
136             return isDestroyed;
137         }
138         catch ( InterruptedException e )
139         {
140             return false;
141         }
142     }
143 }