1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
import java.util.*; |
9 |
|
10 |
/** |
11 |
* Factory and utility methods for the <tt>Executor</tt> classes |
12 |
* defined in <tt>java.util.concurrent</tt>. |
13 |
* |
14 |
* @since 1.5 |
15 |
* @see Executor |
16 |
* @see ExecutorService |
17 |
* @see Future |
18 |
* |
19 |
* @spec JSR-166 |
20 |
* @revised $Date: 2003/08/19 15:04:57 $ |
21 |
* @editor $Author: tim $ |
22 |
* @author Doug Lea |
23 |
*/ |
24 |
public class Executors { |
25 |
|
26 |
/** |
27 |
* A wrapper class that exposes only the ExecutorService methods |
28 |
* of an implementation. |
29 |
*/ |
30 |
private static class DelegatedExecutorService implements ExecutorService { |
31 |
private final ExecutorService e; |
32 |
DelegatedExecutorService(ExecutorService executor) { e = executor; } |
33 |
public void execute(Runnable command) { e.execute(command); } |
34 |
public void shutdown() { e.shutdown(); } |
35 |
public List shutdownNow() { return e.shutdownNow(); } |
36 |
public boolean isShutdown() { return e.isShutdown(); } |
37 |
public boolean isTerminated() { return e.isTerminated(); } |
38 |
public boolean awaitTermination(long timeout, TimeUnit unit) |
39 |
throws InterruptedException { |
40 |
return e.awaitTermination(timeout, unit); |
41 |
} |
42 |
} |
43 |
|
44 |
/** |
45 |
* Creates a thread pool that reuses a fixed set of threads |
46 |
* operating off a shared unbounded queue. If any thread |
47 |
* terminates due to a failure during execution prior to shutdown, |
48 |
* a new one will take its place if needed to execute subsequent |
49 |
* tasks. |
50 |
* |
51 |
* @param nThreads the number of threads in the pool |
52 |
* @return the newly created thread pool |
53 |
*/ |
54 |
public static ExecutorService newFixedThreadPool(int nThreads) { |
55 |
return new DelegatedExecutorService |
56 |
(new ThreadPoolExecutor(nThreads, nThreads, |
57 |
0L, TimeUnit.MILLISECONDS, |
58 |
new LinkedBlockingQueue<Runnable>())); |
59 |
} |
60 |
|
61 |
/** |
62 |
* Creates a thread pool that reuses a fixed set of threads |
63 |
* operating off a shared unbounded queue, using the provided |
64 |
* ThreadFactory to create new threads when needed. |
65 |
* |
66 |
* @param nThreads the number of threads in the pool |
67 |
* @param threadFactory the factory to use when creating new threads |
68 |
* @return the newly created thread pool |
69 |
*/ |
70 |
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { |
71 |
return new DelegatedExecutorService |
72 |
(new ThreadPoolExecutor(nThreads, nThreads, |
73 |
0L, TimeUnit.MILLISECONDS, |
74 |
new LinkedBlockingQueue<Runnable>(), |
75 |
threadFactory)); |
76 |
} |
77 |
|
78 |
/** |
79 |
* Creates an Executor that uses a single worker thread operating |
80 |
* off an unbounded queue. (Note however that if this single |
81 |
* thread terminates due to a failure during execution prior to |
82 |
* shutdown, a new one will take its place if needed to execute |
83 |
* subsequent tasks.) Tasks are guaranteed to execute |
84 |
* sequentially, and no more than one task will be active at any |
85 |
* given time. This method is equivalent in effect to |
86 |
*<tt>new FixedThreadPool(1)</tt>. |
87 |
* |
88 |
* @return the newly-created single-threaded Executor |
89 |
*/ |
90 |
public static ExecutorService newSingleThreadExecutor() { |
91 |
return new DelegatedExecutorService |
92 |
(new ThreadPoolExecutor(1, 1, |
93 |
0L, TimeUnit.MILLISECONDS, |
94 |
new LinkedBlockingQueue<Runnable>())); |
95 |
} |
96 |
|
97 |
/** |
98 |
* Creates an Executor that uses a single worker thread operating |
99 |
* off an unbounded queue, and uses the provided ThreadFactory to |
100 |
* create new threads when needed. |
101 |
* @param threadFactory the factory to use when creating new |
102 |
* threads |
103 |
* |
104 |
* @return the newly-created single-threaded Executor |
105 |
*/ |
106 |
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { |
107 |
return new DelegatedExecutorService |
108 |
(new ThreadPoolExecutor(1, 1, |
109 |
0L, TimeUnit.MILLISECONDS, |
110 |
new LinkedBlockingQueue<Runnable>(), |
111 |
threadFactory)); |
112 |
} |
113 |
|
114 |
/** |
115 |
* Creates a thread pool that creates new threads as needed, but |
116 |
* will reuse previously constructed threads when they are |
117 |
* available. These pools will typically improve the performance |
118 |
* of programs that execute many short-lived asynchronous tasks. |
119 |
* Calls to <tt>execute</tt> will reuse previously constructed |
120 |
* threads if available. If no existing thread is available, a new |
121 |
* thread will be created and added to the pool. Threads that have |
122 |
* not been used for sixty seconds are terminated and removed from |
123 |
* the cache. Thus, a pool that remains idle for long enough will |
124 |
* not consume any resources. Note that pools with similar |
125 |
* properties but different details (for example, timeout parameters) |
126 |
* may be created using {@link ThreadPoolExecutor} constructors. |
127 |
* |
128 |
* @return the newly created thread pool |
129 |
*/ |
130 |
public static ExecutorService newCachedThreadPool() { |
131 |
return new DelegatedExecutorService |
132 |
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
133 |
60, TimeUnit.SECONDS, |
134 |
new SynchronousQueue<Runnable>())); |
135 |
} |
136 |
|
137 |
/** |
138 |
* Creates a thread pool that creates new threads as needed, but |
139 |
* will reuse previously constructed threads when they are |
140 |
* available, and uses the provided |
141 |
* ThreadFactory to create new threads when needed. |
142 |
* @param threadFactory the factory to use when creating new threads |
143 |
* @return the newly created thread pool |
144 |
*/ |
145 |
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { |
146 |
return new DelegatedExecutorService |
147 |
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
148 |
60, TimeUnit.SECONDS, |
149 |
new SynchronousQueue<Runnable>(), |
150 |
threadFactory)); |
151 |
} |
152 |
|
153 |
/** |
154 |
* Executes a Runnable task and returns a Cancellable representing that |
155 |
* task. |
156 |
* |
157 |
* @param executor the Executor to which the task will be submitted |
158 |
* @param task the task to submit |
159 |
* @return a Cancellable representing pending completion of the task |
160 |
* @throws RejectedExecutionException if task cannot be scheduled |
161 |
* for execution |
162 |
*/ |
163 |
public static Cancellable execute(Executor executor, Runnable task) { |
164 |
FutureTask<Boolean> ftask = new FutureTask<Boolean>(task, Boolean.TRUE); |
165 |
executor.execute(ftask); |
166 |
return ftask; |
167 |
} |
168 |
|
169 |
/** |
170 |
* Executes a Runnable task and returns a Future representing that |
171 |
* task. |
172 |
* |
173 |
* @param executor the Executor to which the task will be submitted |
174 |
* @param task the task to submit |
175 |
* @param value the value which will become the return value of |
176 |
* the task upon task completion |
177 |
* @return a Future representing pending completion of the task |
178 |
* @throws RejectedExecutionException if task cannot be scheduled |
179 |
* for execution |
180 |
*/ |
181 |
public static <T> Future<T> execute(Executor executor, Runnable task, T value) { |
182 |
FutureTask<T> ftask = new FutureTask<T>(task, value); |
183 |
executor.execute(ftask); |
184 |
return ftask; |
185 |
} |
186 |
|
187 |
/** |
188 |
* Executes a value-returning task and returns a Future |
189 |
* representing the pending results of the task. |
190 |
* |
191 |
* @param executor the Executor to which the task will be submitted |
192 |
* @param task the task to submit |
193 |
* @return a Future representing pending completion of the task |
194 |
* @throws RejectedExecutionException if task cannot be scheduled |
195 |
* for execution |
196 |
*/ |
197 |
public static <T> Future<T> execute(Executor executor, Callable<T> task) { |
198 |
FutureTask<T> ftask = new FutureTask<T>(task); |
199 |
executor.execute(ftask); |
200 |
return ftask; |
201 |
} |
202 |
|
203 |
/** |
204 |
* Executes a Runnable task and blocks until it completes normally |
205 |
* or throws an exception. |
206 |
* |
207 |
* @param executor the Executor to which the task will be submitted |
208 |
* @param task the task to submit |
209 |
* @throws RejectedExecutionException if task cannot be scheduled |
210 |
* for execution |
211 |
*/ |
212 |
public static void invoke(Executor executor, Runnable task) |
213 |
throws ExecutionException, InterruptedException { |
214 |
FutureTask<Boolean> ftask = new FutureTask<Boolean>(task, Boolean.TRUE); |
215 |
executor.execute(ftask); |
216 |
ftask.get(); |
217 |
} |
218 |
|
219 |
/** |
220 |
* Executes a value-returning task and blocks until it returns a |
221 |
* value or throws an exception. |
222 |
* |
223 |
* @param executor the Executor to which the task will be submitted |
224 |
* @param task the task to submit |
225 |
* @return a Future representing pending completion of the task |
226 |
* @throws RejectedExecutionException if task cannot be scheduled |
227 |
* for execution |
228 |
* @throws InterruptedException if interrupted while waiting for |
229 |
* completion |
230 |
*/ |
231 |
public static <T> T invoke(Executor executor, Callable<T> task) |
232 |
throws ExecutionException, InterruptedException { |
233 |
FutureTask<T> ftask = new FutureTask<T>(task); |
234 |
executor.execute(ftask); |
235 |
return ftask.get(); |
236 |
} |
237 |
|
238 |
|
239 |
/** Cannot instantiate. */ |
240 |
private Executors() {} |
241 |
} |