1 |
tim |
1.1 |
/* |
2 |
dl |
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 |
tim |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
package java.util.concurrent; |
8 |
dl |
1.2 |
import java.util.*; |
9 |
tim |
1.1 |
|
10 |
|
|
/** |
11 |
dl |
1.2 |
* Factory and utility methods for the <tt>Executor</tt> classes |
12 |
|
|
* defined in <tt>java.util.concurrent</tt>. |
13 |
tim |
1.1 |
* |
14 |
|
|
* @since 1.5 |
15 |
|
|
* @see Executor |
16 |
dl |
1.2 |
* @see ExecutorService |
17 |
tim |
1.1 |
* @see Future |
18 |
|
|
* |
19 |
|
|
* @spec JSR-166 |
20 |
dl |
1.14 |
* @revised $Date: 2003/07/31 19:49:42 $ |
21 |
|
|
* @editor $Author: tim $ |
22 |
dl |
1.12 |
* @author Doug Lea |
23 |
tim |
1.1 |
*/ |
24 |
|
|
public class Executors { |
25 |
|
|
|
26 |
|
|
/** |
27 |
dl |
1.2 |
* A wrapper class that exposes only the ExecutorService methods |
28 |
|
|
* of an implementation. |
29 |
tim |
1.6 |
*/ |
30 |
jozart |
1.11 |
private static class DelegatedExecutorService implements ExecutorService { |
31 |
dl |
1.2 |
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 |
tim |
1.1 |
* Creates a thread pool that reuses a fixed set of threads |
46 |
|
|
* operating off a shared unbounded queue. |
47 |
|
|
* |
48 |
|
|
* @param nThreads the number of threads in the pool |
49 |
|
|
* @return the newly created thread pool |
50 |
|
|
*/ |
51 |
dl |
1.2 |
public static ExecutorService newFixedThreadPool(int nThreads) { |
52 |
|
|
return new DelegatedExecutorService |
53 |
|
|
(new ThreadPoolExecutor(nThreads, nThreads, |
54 |
|
|
0L, TimeUnit.MILLISECONDS, |
55 |
dl |
1.3 |
new LinkedBlockingQueue<Runnable>())); |
56 |
dl |
1.2 |
} |
57 |
|
|
|
58 |
|
|
/** |
59 |
|
|
* Creates a thread pool that reuses a fixed set of threads |
60 |
|
|
* operating off a shared unbounded queue, using the provided |
61 |
|
|
* ThreadFactory to create new threads when needed. |
62 |
|
|
* |
63 |
|
|
* @param nThreads the number of threads in the pool |
64 |
dl |
1.12 |
* @param threadFactory the factory to use when creating new threads |
65 |
dl |
1.2 |
* @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 |
dl |
1.3 |
new LinkedBlockingQueue<Runnable>(), |
72 |
dl |
1.14 |
threadFactory)); |
73 |
dl |
1.2 |
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
|
|
* Creates an Executor that uses a single worker thread operating |
77 |
|
|
* off an unbounded queue. (Note however that if this single |
78 |
|
|
* thread terminates due to a failure during execution prior to |
79 |
|
|
* shutdown, a new one will take its place if needed to execute |
80 |
|
|
* subsequent tasks.) Tasks are guaranteed to execute |
81 |
|
|
* sequentially, and no more than one task will be active at any |
82 |
|
|
* given time. |
83 |
|
|
* |
84 |
|
|
* @return the newly-created single-threaded Executor |
85 |
|
|
*/ |
86 |
|
|
public static ExecutorService newSingleThreadExecutor() { |
87 |
|
|
return new DelegatedExecutorService |
88 |
tim |
1.6 |
(new ThreadPoolExecutor(1, 1, |
89 |
dl |
1.2 |
0L, TimeUnit.MILLISECONDS, |
90 |
dl |
1.3 |
new LinkedBlockingQueue<Runnable>())); |
91 |
dl |
1.2 |
} |
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 |
dl |
1.12 |
* @param threadFactory the factory to use when creating new |
98 |
dl |
1.2 |
* threads |
99 |
|
|
* |
100 |
|
|
* @return the newly-created single-threaded Executor |
101 |
|
|
*/ |
102 |
|
|
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { |
103 |
|
|
return new DelegatedExecutorService |
104 |
tim |
1.6 |
(new ThreadPoolExecutor(1, 1, |
105 |
dl |
1.2 |
0L, TimeUnit.MILLISECONDS, |
106 |
dl |
1.3 |
new LinkedBlockingQueue<Runnable>(), |
107 |
dl |
1.14 |
threadFactory)); |
108 |
tim |
1.1 |
} |
109 |
|
|
|
110 |
|
|
/** |
111 |
|
|
* Creates a thread pool that creates new threads as needed, but |
112 |
|
|
* will reuse previously constructed threads when they are |
113 |
|
|
* available. These pools will typically improve the performance |
114 |
|
|
* of programs that execute many short-lived asynchronous tasks. |
115 |
|
|
* Calls to <tt>execute</tt> will reuse previously constructed |
116 |
|
|
* threads if available. If no existing thread is available, a new |
117 |
|
|
* thread will be created and added to the pool. Threads that have |
118 |
|
|
* not been used for sixty seconds are terminated and removed from |
119 |
|
|
* the cache. Thus, a pool that remains idle for long enough will |
120 |
|
|
* not consume any resources. |
121 |
|
|
* |
122 |
|
|
* @return the newly created thread pool |
123 |
|
|
*/ |
124 |
dl |
1.2 |
public static ExecutorService newCachedThreadPool() { |
125 |
|
|
return new DelegatedExecutorService |
126 |
|
|
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
127 |
|
|
60, TimeUnit.SECONDS, |
128 |
dl |
1.3 |
new SynchronousQueue<Runnable>())); |
129 |
tim |
1.1 |
} |
130 |
|
|
|
131 |
|
|
/** |
132 |
dl |
1.2 |
* Creates a thread pool that creates new threads as needed, but |
133 |
|
|
* will reuse previously constructed threads when they are |
134 |
tim |
1.6 |
* available, and uses the provided |
135 |
dl |
1.2 |
* ThreadFactory to create new threads when needed. |
136 |
dl |
1.12 |
* @param threadFactory the factory to use when creating new threads |
137 |
tim |
1.1 |
* @return the newly created thread pool |
138 |
|
|
*/ |
139 |
dl |
1.2 |
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { |
140 |
|
|
return new DelegatedExecutorService |
141 |
|
|
(new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
142 |
|
|
60, TimeUnit.SECONDS, |
143 |
dl |
1.3 |
new SynchronousQueue<Runnable>(), |
144 |
dl |
1.14 |
threadFactory)); |
145 |
tim |
1.1 |
} |
146 |
|
|
|
147 |
|
|
/** |
148 |
|
|
* Executes a Runnable task and returns a Future representing that |
149 |
|
|
* task. |
150 |
|
|
* |
151 |
|
|
* @param executor the Executor to which the task will be submitted |
152 |
|
|
* @param task the task to submit |
153 |
|
|
* @param value the value which will become the return value of |
154 |
|
|
* the task upon task completion |
155 |
dl |
1.5 |
* @return a Future representing pending completion of the task |
156 |
jozart |
1.9 |
* @throws RejectedExecutionException if task cannot be scheduled |
157 |
tim |
1.1 |
* for execution |
158 |
|
|
*/ |
159 |
dl |
1.5 |
public static <T> Future<T> execute(Executor executor, Runnable task, T value) { |
160 |
dl |
1.10 |
FutureTask<T> ftask = new FutureTask<T>(task, value); |
161 |
tim |
1.1 |
executor.execute(ftask); |
162 |
|
|
return ftask; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
/** |
166 |
|
|
* Executes a value-returning task and returns a Future |
167 |
|
|
* representing the pending results of the task. |
168 |
|
|
* |
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 |
jozart |
1.9 |
* @throws RejectedExecutionException if task cannot be scheduled |
173 |
|
|
* for execution |
174 |
tim |
1.1 |
*/ |
175 |
dl |
1.4 |
public static <T> FutureTask<T> execute(Executor executor, Callable<T> task) { |
176 |
dl |
1.10 |
FutureTask<T> ftask = new FutureTask<T>(task); |
177 |
tim |
1.1 |
executor.execute(ftask); |
178 |
|
|
return ftask; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
/** |
182 |
|
|
* Executes a Runnable task and blocks until it completes normally |
183 |
|
|
* or throws an exception. |
184 |
|
|
* |
185 |
|
|
* @param executor the Executor to which the task will be submitted |
186 |
|
|
* @param task the task to submit |
187 |
jozart |
1.9 |
* @throws RejectedExecutionException if task cannot be scheduled |
188 |
|
|
* for execution |
189 |
tim |
1.1 |
*/ |
190 |
|
|
public static void invoke(Executor executor, Runnable task) |
191 |
|
|
throws ExecutionException, InterruptedException { |
192 |
tim |
1.13 |
FutureTask<Boolean> ftask = new FutureTask<Boolean>(task, Boolean.TRUE); |
193 |
tim |
1.1 |
executor.execute(ftask); |
194 |
|
|
ftask.get(); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
/** |
198 |
|
|
* Executes a value-returning task and blocks until it returns a |
199 |
|
|
* value or throws an exception. |
200 |
|
|
* |
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 |
jozart |
1.9 |
* @throws RejectedExecutionException if task cannot be scheduled |
205 |
|
|
* for execution |
206 |
dl |
1.12 |
* @throws InterruptedException if interrupted while waiting for |
207 |
|
|
* completion |
208 |
tim |
1.1 |
*/ |
209 |
|
|
public static <T> T invoke(Executor executor, Callable<T> task) |
210 |
|
|
throws ExecutionException, InterruptedException { |
211 |
|
|
FutureTask<T> ftask = new FutureTask<T>(task); |
212 |
|
|
executor.execute(ftask); |
213 |
|
|
return ftask.get(); |
214 |
tim |
1.6 |
} |
215 |
|
|
|
216 |
tim |
1.1 |
} |