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