39 |
|
return e.awaitTermination(timeout, unit); |
40 |
|
} |
41 |
|
} |
42 |
+ |
|
43 |
+ |
/** |
44 |
+ |
* A wrapper class that exposes only the ExecutorService and |
45 |
+ |
* ScheduleExecutor methods of a ScheduledThreadPoolExecutor. |
46 |
+ |
*/ |
47 |
+ |
private static class DelegatedScheduledExecutorService |
48 |
+ |
extends DelegatedExecutorService |
49 |
+ |
implements ScheduledExecutor { |
50 |
+ |
|
51 |
+ |
private final ScheduledExecutor e; |
52 |
+ |
DelegatedScheduledExecutorService(ScheduledThreadPoolExecutor executor) { |
53 |
+ |
super(executor); |
54 |
+ |
e = executor; |
55 |
+ |
} |
56 |
+ |
public ScheduledFuture<Boolean> schedule(Runnable command, long delay, TimeUnit unit) { |
57 |
+ |
return e.schedule(command, delay, unit); |
58 |
+ |
} |
59 |
+ |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
60 |
+ |
return e.schedule(callable, delay, unit); |
61 |
+ |
} |
62 |
+ |
public ScheduledFuture<Boolean> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { |
63 |
+ |
return e.scheduleAtFixedRate(command, initialDelay, period, unit); |
64 |
+ |
} |
65 |
+ |
public ScheduledFuture<Boolean> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { |
66 |
+ |
return e.scheduleWithFixedDelay(command, initialDelay, delay, unit); |
67 |
+ |
} |
68 |
+ |
} |
69 |
|
|
70 |
|
/** |
71 |
|
* Creates a thread pool that reuses a fixed set of threads |
175 |
|
new SynchronousQueue<Runnable>(), |
176 |
|
threadFactory)); |
177 |
|
} |
178 |
+ |
|
179 |
+ |
|
180 |
+ |
/** |
181 |
+ |
* Creates a thread pool that can schedule commands to run after a |
182 |
+ |
* given delay, or to execute periodically. |
183 |
+ |
* @return a <tt>ScheduledExecutor</tt> that may safely be cast to |
184 |
+ |
* an <tt>ExecutorService</tt>. |
185 |
+ |
*/ |
186 |
+ |
public static ScheduledExecutor newScheduledThreadPool() { |
187 |
+ |
return newScheduledThreadPool(0); |
188 |
+ |
} |
189 |
+ |
|
190 |
+ |
|
191 |
+ |
/** |
192 |
+ |
* Creates a thread pool that can schedule commands to run after a |
193 |
+ |
* given delay, or to execute periodically. |
194 |
+ |
* @param corePoolSize the number of threads to keep in the pool, |
195 |
+ |
* even if they are idle. |
196 |
+ |
* @return a <tt>ScheduledExecutor</tt> that may safely be cast to |
197 |
+ |
* an <tt>ExecutorService</tt>. |
198 |
+ |
*/ |
199 |
+ |
public static ScheduledExecutor newScheduledThreadPool(int corePoolSize) { |
200 |
+ |
return newScheduledThreadPool(corePoolSize, null); |
201 |
+ |
} |
202 |
+ |
|
203 |
+ |
|
204 |
+ |
/** |
205 |
+ |
* Creates a thread pool that can schedule commands to run after a |
206 |
+ |
* given delay, or to execute periodically. |
207 |
+ |
* @param corePoolSize the number of threads to keep in the pool, |
208 |
+ |
* even if they are idle. |
209 |
+ |
* @param threadFactory the factory to use when the executor |
210 |
+ |
* creates a new thread. |
211 |
+ |
* @return a <tt>ScheduledExecutor</tt> that may safely be cast to |
212 |
+ |
* an <tt>ExecutorService</tt>. |
213 |
+ |
*/ |
214 |
+ |
public static ScheduledExecutor newScheduledThreadPool(int corePoolSize, |
215 |
+ |
ThreadFactory threadFactory) { |
216 |
+ |
return newScheduledThreadPool(corePoolSize, threadFactory, false, false); |
217 |
+ |
} |
218 |
+ |
|
219 |
+ |
|
220 |
+ |
/** |
221 |
+ |
* Creates a thread pool that can schedule commands to run after a |
222 |
+ |
* given delay, or to execute periodically. |
223 |
+ |
* @param corePoolSize the number of threads to keep in the pool, |
224 |
+ |
* even if they are idle. |
225 |
+ |
* @param threadFactory the factory to use when the executor |
226 |
+ |
* creates a new thread. |
227 |
+ |
* @param continueExistingPeriodicTasksAfterShutdown whether to |
228 |
+ |
* continue executing existing periodic tasks even when the returned |
229 |
+ |
* executor has been <tt>shutdown</tt>. |
230 |
+ |
* @param executeExistingDelayedTasksAfterShutdown whether to |
231 |
+ |
* continue executing existing delayed tasks even when the returned |
232 |
+ |
* executor has been <tt>shutdown</tt>. |
233 |
+ |
* @return a <tt>ScheduledExecutor</tt> that may safely be cast to |
234 |
+ |
* an <tt>ExecutorService</tt>. |
235 |
+ |
*/ |
236 |
+ |
public static ScheduledExecutor newScheduledThreadPool( |
237 |
+ |
int corePoolSize, |
238 |
+ |
ThreadFactory threadFactory, |
239 |
+ |
boolean continueExistingPeriodicTasksAfterShutdown, |
240 |
+ |
boolean executeExistingDelayedTasksAfterShutdown) { |
241 |
+ |
|
242 |
+ |
ScheduledThreadPoolExecutor stpe = threadFactory == null ? |
243 |
+ |
new ScheduledThreadPoolExecutor(corePoolSize) : |
244 |
+ |
new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); |
245 |
+ |
|
246 |
+ |
stpe.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
247 |
+ |
continueExistingPeriodicTasksAfterShutdown); |
248 |
+ |
|
249 |
+ |
stpe.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
250 |
+ |
executeExistingDelayedTasksAfterShutdown); |
251 |
+ |
|
252 |
+ |
return new DelegatedScheduledExecutorService(stpe); |
253 |
+ |
} |
254 |
+ |
|
255 |
|
|
256 |
|
/** |
257 |
|
* Executes a Runnable task and returns a Future representing that |
417 |
|
* @return the thread factory |
418 |
|
*/ |
419 |
|
public static ThreadFactory defaultThreadFactory() { |
420 |
< |
return new DefaultThreadFactory(); |
420 |
> |
return new DefaultThreadFactory(); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
454 |
|
* @see PrivilegedFutureTask |
455 |
|
*/ |
456 |
|
public static ThreadFactory privilegedThreadFactory() { |
457 |
< |
return new PrivilegedThreadFactory(); |
457 |
> |
return new PrivilegedThreadFactory(); |
458 |
|
} |
459 |
|
|
460 |
|
static class DefaultThreadFactory implements ThreadFactory { |
461 |
< |
static final AtomicInteger poolNumber = new AtomicInteger(1); |
462 |
< |
final ThreadGroup group; |
463 |
< |
final AtomicInteger threadNumber = new AtomicInteger(1); |
464 |
< |
final String namePrefix; |
461 |
> |
static final AtomicInteger poolNumber = new AtomicInteger(1); |
462 |
> |
final ThreadGroup group; |
463 |
> |
final AtomicInteger threadNumber = new AtomicInteger(1); |
464 |
> |
final String namePrefix; |
465 |
|
|
466 |
< |
DefaultThreadFactory() { |
466 |
> |
DefaultThreadFactory() { |
467 |
|
SecurityManager s = System.getSecurityManager(); |
468 |
|
group = (s != null)? s.getThreadGroup() : |
469 |
|
Thread.currentThread().getThreadGroup(); |