11 |
|
* Factory and utility methods for the <tt>Executor</tt> classes |
12 |
|
* defined in <tt>java.util.concurrent</tt>. |
13 |
|
* |
14 |
– |
* <p>An Executor is a framework for executing Runnables. The |
15 |
– |
* Executor manages queueing and scheduling of tasks, and creation and |
16 |
– |
* teardown of threads. Depending on which concrete Executor class is |
17 |
– |
* being used, tasks may execute in a newly created thread, an |
18 |
– |
* existing task-execution thread, or the thread calling execute(), |
19 |
– |
* and may execute sequentially or concurrently. |
20 |
– |
* |
14 |
|
* @since 1.5 |
15 |
|
* @see Executor |
16 |
|
* @see ExecutorService |
19 |
|
* @spec JSR-166 |
20 |
|
* @revised $Date$ |
21 |
|
* @editor $Author$ |
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 |
< |
static private class DelegatedExecutorService implements ExecutorService { |
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); } |
52 |
|
return new DelegatedExecutorService |
53 |
|
(new ThreadPoolExecutor(nThreads, nThreads, |
54 |
|
0L, TimeUnit.MILLISECONDS, |
55 |
< |
new LinkedBlockingQueue())); |
55 |
> |
new LinkedBlockingQueue<Runnable>())); |
56 |
|
} |
57 |
|
|
58 |
|
/** |
61 |
|
* ThreadFactory to create new threads when needed. |
62 |
|
* |
63 |
|
* @param nThreads the number of threads in the pool |
64 |
< |
* @param threadfactory the factory to use when creating new threads |
64 |
> |
* @param threadFactory the factory to use when creating new threads |
65 |
|
* @return the newly created thread pool |
66 |
|
*/ |
67 |
|
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { |
68 |
|
return new DelegatedExecutorService |
69 |
|
(new ThreadPoolExecutor(nThreads, nThreads, |
70 |
|
0L, TimeUnit.MILLISECONDS, |
71 |
< |
new LinkedBlockingQueue(), |
71 |
> |
new LinkedBlockingQueue<Runnable>(), |
72 |
|
threadFactory, null)); |
73 |
|
} |
74 |
|
|
85 |
|
*/ |
86 |
|
public static ExecutorService newSingleThreadExecutor() { |
87 |
|
return new DelegatedExecutorService |
88 |
< |
(new ThreadPoolExecutor(1, 1, |
88 |
> |
(new ThreadPoolExecutor(1, 1, |
89 |
|
0L, TimeUnit.MILLISECONDS, |
90 |
< |
new LinkedBlockingQueue())); |
90 |
> |
new LinkedBlockingQueue<Runnable>())); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
|
* Creates an Executor that uses a single worker thread operating |
95 |
|
* off an unbounded queue, and uses the provided ThreadFactory to |
96 |
|
* create new threads when needed. |
97 |
< |
* @param threadfactory the factory to use when creating new |
97 |
> |
* @param threadFactory the factory to use when creating new |
98 |
|
* threads |
99 |
|
* |
100 |
|
* @return the newly-created single-threaded Executor |
101 |
|
*/ |
102 |
|
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { |
103 |
|
return new DelegatedExecutorService |
104 |
< |
(new ThreadPoolExecutor(1, 1, |
104 |
> |
(new ThreadPoolExecutor(1, 1, |
105 |
|
0L, TimeUnit.MILLISECONDS, |
106 |
< |
new LinkedBlockingQueue(), |
106 |
> |
new LinkedBlockingQueue<Runnable>(), |
107 |
|
threadFactory, null)); |
108 |
|
} |
109 |
|
|
125 |
|
return new DelegatedExecutorService |
126 |
|
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
127 |
|
60, TimeUnit.SECONDS, |
128 |
< |
new SynchronousQueue())); |
128 |
> |
new SynchronousQueue<Runnable>())); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
|
* Creates a thread pool that creates new threads as needed, but |
133 |
|
* will reuse previously constructed threads when they are |
134 |
< |
* available, and uses the provided |
134 |
> |
* available, and uses the provided |
135 |
|
* ThreadFactory to create new threads when needed. |
136 |
< |
* @param threadfactory the factory to use when creating new threads |
136 |
> |
* @param threadFactory the factory to use when creating new threads |
137 |
|
* @return the newly created thread pool |
138 |
|
*/ |
139 |
|
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { |
140 |
|
return new DelegatedExecutorService |
141 |
|
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
142 |
|
60, TimeUnit.SECONDS, |
143 |
< |
new SynchronousQueue(), |
143 |
> |
new SynchronousQueue<Runnable>(), |
144 |
|
threadFactory, null)); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
154 |
– |
* Constructs a ScheduledExecutor. A ScheduledExecutor is an |
155 |
– |
* Executor which can schedule tasks to run at a given future |
156 |
– |
* time, or to execute periodically. |
157 |
– |
* |
158 |
– |
* @param minThreads the minimum number of threads to keep in the |
159 |
– |
* pool, even if they are idle. |
160 |
– |
* @param maxThreads the maximum number of threads to allow in the |
161 |
– |
* pool. |
162 |
– |
* @param keepAliveTime when the number of threads is greater than |
163 |
– |
* the minimum, this is the maximum time that excess idle threads |
164 |
– |
* will wait for new tasks before terminating. |
165 |
– |
* @param unit the time unit for the keepAliveTime |
166 |
– |
* argument. |
167 |
– |
* @return the newly created ScheduledExecutor |
168 |
– |
*/ |
169 |
– |
// public static ScheduledExecutor newScheduledExecutor(int minThreads, |
170 |
– |
// int maxThreads, |
171 |
– |
// long keepAliveTime, |
172 |
– |
// TimeUnit unit) { |
173 |
– |
// return new ScheduledExecutor(minThreads, maxThreads, |
174 |
– |
// keepAliveTime, unit); |
175 |
– |
// } |
176 |
– |
|
177 |
– |
/** |
148 |
|
* Executes a Runnable task and returns a Future representing that |
149 |
|
* task. |
150 |
|
* |
153 |
|
* @param value the value which will become the return value of |
154 |
|
* the task upon task completion |
155 |
|
* @return a Future representing pending completion of the task |
156 |
< |
* @throws CannotExecuteException if the task cannot be scheduled |
156 |
> |
* @throws RejectedExecutionException if task cannot be scheduled |
157 |
|
* for execution |
158 |
|
*/ |
159 |
|
public static <T> Future<T> execute(Executor executor, Runnable task, T value) { |
169 |
|
* @param executor the Executor to which the task will be submitted |
170 |
|
* @param task the task to submit |
171 |
|
* @return a Future representing pending completion of the task |
172 |
< |
* @throws CannotExecuteException if task cannot be scheduled for execution |
172 |
> |
* @throws RejectedExecutionException if task cannot be scheduled |
173 |
> |
* for execution |
174 |
|
*/ |
175 |
< |
public static <T> Future<T> execute(Executor executor, Callable<T> task) { |
175 |
> |
public static <T> FutureTask<T> execute(Executor executor, Callable<T> task) { |
176 |
|
FutureTask<T> ftask = new FutureTask<T>(task); |
177 |
|
executor.execute(ftask); |
178 |
|
return ftask; |
184 |
|
* |
185 |
|
* @param executor the Executor to which the task will be submitted |
186 |
|
* @param task the task to submit |
187 |
< |
* @throws CannotExecuteException if task cannot be scheduled for execution |
187 |
> |
* @throws RejectedExecutionException if task cannot be scheduled |
188 |
> |
* for execution |
189 |
|
*/ |
190 |
|
public static void invoke(Executor executor, Runnable task) |
191 |
|
throws ExecutionException, InterruptedException { |
192 |
< |
FutureTask ftask = new FutureTask(task, Boolean.TRUE); |
192 |
> |
FutureTask<Boolean> ftask = new FutureTask(task, Boolean.TRUE); |
193 |
|
executor.execute(ftask); |
194 |
|
ftask.get(); |
195 |
|
} |
201 |
|
* @param executor the Executor to which the task will be submitted |
202 |
|
* @param task the task to submit |
203 |
|
* @return a Future representing pending completion of the task |
204 |
< |
* @throws CannotExecuteException if task cannot be scheduled for execution |
204 |
> |
* @throws RejectedExecutionException if task cannot be scheduled |
205 |
> |
* for execution |
206 |
> |
* @throws InterruptedException if interrupted while waiting for |
207 |
> |
* completion |
208 |
|
*/ |
209 |
|
public static <T> T invoke(Executor executor, Callable<T> task) |
210 |
|
throws ExecutionException, InterruptedException { |
212 |
|
executor.execute(ftask); |
213 |
|
return ftask.get(); |
214 |
|
} |
215 |
+ |
|
216 |
|
} |