1 package org.apache.maven.surefire.junitcore.pc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.report.ConsoleStream;
23
24 import java.util.Collection;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.ThreadPoolExecutor;
28 import java.util.concurrent.TimeUnit;
29
30
31
32
33
34
35
36
37
38
39
40
41 abstract class AbstractThreadPoolStrategy
42 extends SchedulingStrategy
43 {
44 private final ExecutorService threadPool;
45
46 private final Collection<Future<?>> futureResults;
47
48 private volatile boolean isDestroyed;
49
50 AbstractThreadPoolStrategy( ConsoleStream logger, ExecutorService threadPool )
51 {
52 this( logger, threadPool, null );
53 }
54
55 AbstractThreadPoolStrategy( ConsoleStream logger, ExecutorService threadPool, Collection<Future<?>> futureResults )
56 {
57 super( logger );
58 this.threadPool = threadPool;
59 this.futureResults = futureResults;
60 }
61
62 protected final ExecutorService getThreadPool()
63 {
64 return threadPool;
65 }
66
67 protected final Collection<Future<?>> getFutureResults()
68 {
69 return futureResults;
70 }
71
72 @Override
73 public void schedule( Runnable task )
74 {
75 if ( canSchedule() )
76 {
77 Future<?> futureResult = threadPool.submit( task );
78 if ( futureResults != null )
79 {
80 futureResults.add( futureResult );
81 }
82 }
83 }
84
85 @Override
86 protected boolean stop()
87 {
88 boolean wasRunning = disable();
89 if ( threadPool.isShutdown() )
90 {
91 wasRunning = false;
92 }
93 else
94 {
95 threadPool.shutdown();
96 }
97 return wasRunning;
98 }
99
100 @Override
101 protected boolean stopNow()
102 {
103 boolean wasRunning = disable();
104 if ( threadPool.isShutdown() )
105 {
106 wasRunning = false;
107 }
108 else
109 {
110 threadPool.shutdownNow();
111 }
112 return wasRunning;
113 }
114
115
116
117
118 @Override
119 protected void setDefaultShutdownHandler( Scheduler.ShutdownHandler handler )
120 {
121 if ( threadPool instanceof ThreadPoolExecutor )
122 {
123 ThreadPoolExecutor pool = (ThreadPoolExecutor) threadPool;
124 handler.setRejectedExecutionHandler( pool.getRejectedExecutionHandler() );
125 pool.setRejectedExecutionHandler( handler );
126 }
127 }
128
129 @Override
130 public boolean destroy()
131 {
132 try
133 {
134 if ( !isDestroyed )
135 {
136 disable();
137 threadPool.shutdown();
138 this.isDestroyed |= threadPool.awaitTermination( Long.MAX_VALUE, TimeUnit.NANOSECONDS );
139 }
140 return isDestroyed;
141 }
142 catch ( InterruptedException e )
143 {
144 return false;
145 }
146 }
147 }