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 |
* <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 |
* |
21 |
* @since 1.5 |
22 |
* @see Executor |
23 |
* @see ExecutorService |
24 |
* @see Future |
25 |
* |
26 |
* @spec JSR-166 |
27 |
* @revised $Date: 2003/05/29 13:49:24 $ |
28 |
* @editor $Author: dl $ |
29 |
*/ |
30 |
public class Executors { |
31 |
|
32 |
/** |
33 |
* A wrapper class that exposes only the ExecutorService methods |
34 |
* of an implementation. |
35 |
*/ |
36 |
static private class DelegatedExecutorService implements ExecutorService { |
37 |
private final ExecutorService e; |
38 |
DelegatedExecutorService(ExecutorService executor) { e = executor; } |
39 |
public void execute(Runnable command) { e.execute(command); } |
40 |
public void shutdown() { e.shutdown(); } |
41 |
public List shutdownNow() { return e.shutdownNow(); } |
42 |
public boolean isShutdown() { return e.isShutdown(); } |
43 |
public boolean isTerminated() { return e.isTerminated(); } |
44 |
public boolean remove(Runnable r) { return e.remove(r) ; } |
45 |
public boolean awaitTermination(long timeout, TimeUnit unit) |
46 |
throws InterruptedException { |
47 |
return e.awaitTermination(timeout, unit); |
48 |
} |
49 |
} |
50 |
|
51 |
/** |
52 |
* Creates a thread pool that reuses a fixed set of threads |
53 |
* operating off a shared unbounded queue. |
54 |
* |
55 |
* @param nThreads the number of threads in the pool |
56 |
* @return the newly created thread pool |
57 |
*/ |
58 |
public static ExecutorService newFixedThreadPool(int nThreads) { |
59 |
return new DelegatedExecutorService |
60 |
(new ThreadPoolExecutor(nThreads, nThreads, |
61 |
0L, TimeUnit.MILLISECONDS, |
62 |
new LinkedBlockingQueue<Runnable>())); |
63 |
} |
64 |
|
65 |
/** |
66 |
* Creates a thread pool that reuses a fixed set of threads |
67 |
* operating off a shared unbounded queue, using the provided |
68 |
* ThreadFactory to create new threads when needed. |
69 |
* |
70 |
* @param nThreads the number of threads in the pool |
71 |
* @param threadfactory the factory to use when creating new threads |
72 |
* @return the newly created thread pool |
73 |
*/ |
74 |
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { |
75 |
return new DelegatedExecutorService |
76 |
(new ThreadPoolExecutor(nThreads, nThreads, |
77 |
0L, TimeUnit.MILLISECONDS, |
78 |
new LinkedBlockingQueue<Runnable>(), |
79 |
threadFactory, null)); |
80 |
} |
81 |
|
82 |
/** |
83 |
* Creates an Executor that uses a single worker thread operating |
84 |
* off an unbounded queue. (Note however that if this single |
85 |
* thread terminates due to a failure during execution prior to |
86 |
* shutdown, a new one will take its place if needed to execute |
87 |
* subsequent tasks.) Tasks are guaranteed to execute |
88 |
* sequentially, and no more than one task will be active at any |
89 |
* given time. |
90 |
* |
91 |
* @return the newly-created single-threaded Executor |
92 |
*/ |
93 |
public static ExecutorService newSingleThreadExecutor() { |
94 |
return new DelegatedExecutorService |
95 |
(new ThreadPoolExecutor(1, 1, |
96 |
0L, TimeUnit.MILLISECONDS, |
97 |
new LinkedBlockingQueue<Runnable>())); |
98 |
} |
99 |
|
100 |
/** |
101 |
* Creates an Executor that uses a single worker thread operating |
102 |
* off an unbounded queue, and uses the provided ThreadFactory to |
103 |
* create new threads when needed. |
104 |
* @param threadfactory the factory to use when creating new |
105 |
* threads |
106 |
* |
107 |
* @return the newly-created single-threaded Executor |
108 |
*/ |
109 |
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { |
110 |
return new DelegatedExecutorService |
111 |
(new ThreadPoolExecutor(1, 1, |
112 |
0L, TimeUnit.MILLISECONDS, |
113 |
new LinkedBlockingQueue<Runnable>(), |
114 |
threadFactory, null)); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Creates a thread pool that creates new threads as needed, but |
119 |
* will reuse previously constructed threads when they are |
120 |
* available. These pools will typically improve the performance |
121 |
* of programs that execute many short-lived asynchronous tasks. |
122 |
* Calls to <tt>execute</tt> will reuse previously constructed |
123 |
* threads if available. If no existing thread is available, a new |
124 |
* thread will be created and added to the pool. Threads that have |
125 |
* not been used for sixty seconds are terminated and removed from |
126 |
* the cache. Thus, a pool that remains idle for long enough will |
127 |
* not consume any resources. |
128 |
* |
129 |
* @return the newly created thread pool |
130 |
*/ |
131 |
public static ExecutorService newCachedThreadPool() { |
132 |
return new DelegatedExecutorService |
133 |
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
134 |
60, TimeUnit.SECONDS, |
135 |
new SynchronousQueue<Runnable>())); |
136 |
} |
137 |
|
138 |
/** |
139 |
* Creates a thread pool that creates new threads as needed, but |
140 |
* will reuse previously constructed threads when they are |
141 |
* available, and uses the provided |
142 |
* ThreadFactory to create new threads when needed. |
143 |
* @param threadfactory the factory to use when creating new threads |
144 |
* @return the newly created thread pool |
145 |
*/ |
146 |
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { |
147 |
return new DelegatedExecutorService |
148 |
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
149 |
60, TimeUnit.SECONDS, |
150 |
new SynchronousQueue<Runnable>(), |
151 |
threadFactory, null)); |
152 |
} |
153 |
|
154 |
/** |
155 |
* Executes a Runnable task and returns a Future representing that |
156 |
* task. |
157 |
* |
158 |
* @param executor the Executor to which the task will be submitted |
159 |
* @param task the task to submit |
160 |
* @param value the value which will become the return value of |
161 |
* the task upon task completion |
162 |
* @return a CancellableTask that allows cancellation. |
163 |
* @throws CannotExecuteException if the task cannot be scheduled |
164 |
* for execution |
165 |
*/ |
166 |
public static CancellableTask execute(Executor executor, Runnable task) { |
167 |
CancellableTask ftask = new CancellableTask(task); |
168 |
executor.execute(ftask); |
169 |
return ftask; |
170 |
} |
171 |
|
172 |
/** |
173 |
* Executes a value-returning task and returns a Future |
174 |
* representing the pending results of the task. |
175 |
* |
176 |
* @param executor the Executor to which the task will be submitted |
177 |
* @param task the task to submit |
178 |
* @return a Future representing pending completion of the task |
179 |
* @throws CannotExecuteException if task cannot be scheduled for execution |
180 |
*/ |
181 |
public static <T> FutureTask<T> execute(Executor executor, Callable<T> task) { |
182 |
FutureTask<T> ftask = new FutureTask<T>(task); |
183 |
executor.execute(ftask); |
184 |
return ftask; |
185 |
} |
186 |
|
187 |
/** |
188 |
* Executes a Runnable task and blocks until it completes normally |
189 |
* or throws an exception. |
190 |
* |
191 |
* @param executor the Executor to which the task will be submitted |
192 |
* @param task the task to submit |
193 |
* @throws CannotExecuteException if task cannot be scheduled for execution |
194 |
*/ |
195 |
public static void invoke(Executor executor, Runnable task) |
196 |
throws ExecutionException, InterruptedException { |
197 |
FutureTask<Boolean> ftask = new FutureTask(task, Boolean.TRUE); |
198 |
executor.execute(ftask); |
199 |
ftask.get(); |
200 |
} |
201 |
|
202 |
/** |
203 |
* Executes a value-returning task and blocks until it returns a |
204 |
* value or throws an exception. |
205 |
* |
206 |
* @param executor the Executor to which the task will be submitted |
207 |
* @param task the task to submit |
208 |
* @return a Future representing pending completion of the task |
209 |
* @throws CannotExecuteException if task cannot be scheduled for execution |
210 |
*/ |
211 |
public static <T> T invoke(Executor executor, Callable<T> task) |
212 |
throws ExecutionException, InterruptedException { |
213 |
FutureTask<T> ftask = new FutureTask<T>(task); |
214 |
executor.execute(ftask); |
215 |
return ftask.get(); |
216 |
} |
217 |
} |