42 |
|
/** |
43 |
|
* Creates a thread pool that reuses a fixed number of threads |
44 |
|
* operating off a shared unbounded queue. At any point, at most |
45 |
< |
* <tt>nThreads</tt> threads will be active processing tasks. If |
46 |
< |
* additional tasks are submitted when all threads are active, |
47 |
< |
* they will wait in the queue until a thread is available. If |
48 |
< |
* any thread terminates due to a failure during execution prior |
49 |
< |
* to shutdown, a new one will take its place if needed to execute |
50 |
< |
* subsequent tasks. The threads in the pool will exist until it |
51 |
< |
* is explicitly {@link ExecutorService#shutdown shutdown}. |
45 |
> |
* <tt>nThreads</tt> threads will be active processing tasks. |
46 |
> |
* If additional tasks are submitted when all threads are active, |
47 |
> |
* they will wait in the queue until a thread is available. |
48 |
> |
* If any thread terminates due to a failure during execution |
49 |
> |
* prior to shutdown, a new one will take its place if needed to |
50 |
> |
* execute subsequent tasks. The threads in the pool will exist |
51 |
> |
* until it is explicitly {@link ExecutorService#shutdown shutdown}. |
52 |
|
* |
53 |
|
* @param nThreads the number of threads in the pool |
54 |
|
* @return the newly created thread pool |
55 |
+ |
* @throws IllegalArgumentException if <tt>nThreads <= 0</tt> |
56 |
|
*/ |
57 |
|
public static ExecutorService newFixedThreadPool(int nThreads) { |
58 |
|
return new ThreadPoolExecutor(nThreads, nThreads, |
69 |
|
* active, they will wait in the queue until a thread is |
70 |
|
* available. If any thread terminates due to a failure during |
71 |
|
* execution prior to shutdown, a new one will take its place if |
72 |
< |
* needed to execute subsequent tasks. The threads in the pool |
73 |
< |
* will exist until it is explicitly {@link |
74 |
< |
* ExecutorService#shutdown shutdown}. |
72 |
> |
* needed to execute subsequent tasks. The threads in the pool will |
73 |
> |
* exist until it is explicitly {@link ExecutorService#shutdown |
74 |
> |
* shutdown}. |
75 |
|
* |
76 |
|
* @param nThreads the number of threads in the pool |
77 |
|
* @param threadFactory the factory to use when creating new threads |
78 |
|
* @return the newly created thread pool |
79 |
+ |
* @throws NullPointerException if threadFactory is null |
80 |
+ |
* @throws IllegalArgumentException if <tt>nThreads <= 0</tt> |
81 |
|
*/ |
82 |
|
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { |
83 |
|
return new ThreadPoolExecutor(nThreads, nThreads, |
118 |
|
* threads |
119 |
|
* |
120 |
|
* @return the newly created single-threaded Executor |
121 |
+ |
* @throws NullPointerException if threadFactory is null |
122 |
|
*/ |
123 |
|
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { |
124 |
|
return new FinalizableDelegatedExecutorService |
157 |
|
* ThreadFactory to create new threads when needed. |
158 |
|
* @param threadFactory the factory to use when creating new threads |
159 |
|
* @return the newly created thread pool |
160 |
+ |
* @throws NullPointerException if threadFactory is null |
161 |
|
*/ |
162 |
|
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { |
163 |
|
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
198 |
|
* @param threadFactory the factory to use when creating new |
199 |
|
* threads |
200 |
|
* @return a newly created scheduled executor |
201 |
+ |
* @throws NullPointerException if threadFactory is null |
202 |
|
*/ |
203 |
|
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { |
204 |
|
return new DelegatedScheduledExecutorService |
211 |
|
* @param corePoolSize the number of threads to keep in the pool, |
212 |
|
* even if they are idle. |
213 |
|
* @return a newly created scheduled thread pool |
214 |
+ |
* @throws IllegalArgumentException if <tt>corePoolSize < 0</tt> |
215 |
|
*/ |
216 |
|
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { |
217 |
|
return new ScheduledThreadPoolExecutor(corePoolSize); |
225 |
|
* @param threadFactory the factory to use when the executor |
226 |
|
* creates a new thread. |
227 |
|
* @return a newly created scheduled thread pool |
228 |
+ |
* @throws IllegalArgumentException if <tt>corePoolSize < 0</tt> |
229 |
+ |
* @throws NullPointerException if threadFactory is null |
230 |
|
*/ |
231 |
|
public static ScheduledExecutorService newScheduledThreadPool( |
232 |
|
int corePoolSize, ThreadFactory threadFactory) { |
329 |
|
* <tt>Callable</tt> to an otherwise resultless action. |
330 |
|
* @param task the task to run |
331 |
|
* @param result the result to return |
323 |
– |
* @throws NullPointerException if task null |
332 |
|
* @return a callable object |
333 |
+ |
* @throws NullPointerException if task null |
334 |
|
*/ |
335 |
|
public static <T> Callable<T> callable(Runnable task, T result) { |
336 |
|
if (task == null) |
625 |
|
} |
626 |
|
|
627 |
|
static class FinalizableDelegatedExecutorService |
628 |
< |
extends DelegatedExecutorService { |
629 |
< |
FinalizableDelegatedExecutorService(ExecutorService executor) { |
630 |
< |
super(executor); |
631 |
< |
} |
632 |
< |
protected void finalize() { |
633 |
< |
super.shutdown(); |
634 |
< |
} |
628 |
> |
extends DelegatedExecutorService { |
629 |
> |
FinalizableDelegatedExecutorService(ExecutorService executor) { |
630 |
> |
super(executor); |
631 |
> |
} |
632 |
> |
protected void finalize() { |
633 |
> |
super.shutdown(); |
634 |
> |
} |
635 |
|
} |
627 |
– |
|
636 |
|
|
637 |
|
/** |
638 |
|
* A wrapper class that exposes only the ScheduledExecutorService |