5 |
|
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
< |
|
8 |
> |
import java.util.concurrent.*; // for javadoc (till 6280605 is fixed) |
9 |
|
import java.util.List; |
10 |
|
import java.util.Collection; |
11 |
|
import java.security.PrivilegedAction; |
43 |
|
* |
44 |
|
* <pre> |
45 |
|
* class NetworkService { |
46 |
< |
* private final ServerSocket serverSocket; |
47 |
< |
* private final ExecutorService pool; |
46 |
> |
* private final ServerSocket serverSocket; |
47 |
> |
* private final ExecutorService pool; |
48 |
|
* |
49 |
< |
* public NetworkService(int port, int poolSize) throws IOException { |
50 |
< |
* serverSocket = new ServerSocket(port); |
51 |
< |
* pool = Executors.newFixedThreadPool(poolSize); |
52 |
< |
* } |
49 |
> |
* public NetworkService(int port, int poolSize) |
50 |
> |
* throws IOException { |
51 |
> |
* serverSocket = new ServerSocket(port); |
52 |
> |
* pool = Executors.newFixedThreadPool(poolSize); |
53 |
> |
* } |
54 |
|
* |
55 |
< |
* public void serve() { |
56 |
< |
* try { |
57 |
< |
* for (;;) { |
58 |
< |
* pool.execute(new Handler(serverSocket.accept())); |
59 |
< |
* } |
60 |
< |
* } catch (IOException ex) { |
61 |
< |
* pool.shutdown(); |
62 |
< |
* } |
63 |
< |
* } |
64 |
< |
* } |
55 |
> |
* public void serve() { |
56 |
> |
* try { |
57 |
> |
* for (;;) { |
58 |
> |
* pool.execute(new Handler(serverSocket.accept())); |
59 |
> |
* } |
60 |
> |
* } catch (IOException ex) { |
61 |
> |
* pool.shutdown(); |
62 |
> |
* } |
63 |
> |
* } |
64 |
> |
* } |
65 |
|
* |
66 |
< |
* class Handler implements Runnable { |
67 |
< |
* private final Socket socket; |
68 |
< |
* Handler(Socket socket) { this.socket = socket; } |
69 |
< |
* public void run() { |
70 |
< |
* // read and service request |
71 |
< |
* } |
66 |
> |
* class Handler implements Runnable { |
67 |
> |
* private final Socket socket; |
68 |
> |
* Handler(Socket socket) { this.socket = socket; } |
69 |
> |
* public void run() { |
70 |
> |
* // read and service request |
71 |
> |
* } |
72 |
|
* } |
73 |
|
* </pre> |
74 |
|
* @since 1.5 |
78 |
|
|
79 |
|
/** |
80 |
|
* Initiates an orderly shutdown in which previously submitted |
81 |
< |
* tasks are executed, but no new tasks will be |
82 |
< |
* accepted. Invocation has no additional effect if already shut |
83 |
< |
* down. |
81 |
> |
* tasks are executed, but no new tasks will be accepted. |
82 |
> |
* Invocation has no additional effect if already shut down. |
83 |
> |
* |
84 |
|
* @throws SecurityException if a security manager exists and |
85 |
< |
* shutting down this ExecutorService may manipulate threads that |
86 |
< |
* the caller is not permitted to modify because it does not hold |
87 |
< |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
88 |
< |
* or the security manager's <tt>checkAccess</tt> method denies access. |
85 |
> |
* shutting down this ExecutorService may manipulate |
86 |
> |
* threads that the caller is not permitted to modify |
87 |
> |
* because it does not hold {@link |
88 |
> |
* java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
89 |
> |
* or the security manager's <tt>checkAccess</tt> method |
90 |
> |
* denies access. |
91 |
|
*/ |
92 |
|
void shutdown(); |
93 |
|
|
103 |
|
* |
104 |
|
* @return list of tasks that never commenced execution |
105 |
|
* @throws SecurityException if a security manager exists and |
106 |
< |
* shutting down this ExecutorService may manipulate threads that |
107 |
< |
* the caller is not permitted to modify because it does not hold |
108 |
< |
* {@link java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
109 |
< |
* or the security manager's <tt>checkAccess</tt> method denies access. |
106 |
> |
* shutting down this ExecutorService may manipulate |
107 |
> |
* threads that the caller is not permitted to modify |
108 |
> |
* because it does not hold {@link |
109 |
> |
* java.lang.RuntimePermission}<tt>("modifyThread")</tt>, |
110 |
> |
* or the security manager's <tt>checkAccess</tt> method |
111 |
> |
* denies access. |
112 |
|
*/ |
113 |
|
List<Runnable> shutdownNow(); |
114 |
|
|
135 |
|
* |
136 |
|
* @param timeout the maximum time to wait |
137 |
|
* @param unit the time unit of the timeout argument |
138 |
< |
* @return <tt>true</tt> if this executor terminated and <tt>false</tt> |
139 |
< |
* if the timeout elapsed before termination |
138 |
> |
* @return <tt>true</tt> if this executor terminated and |
139 |
> |
* <tt>false</tt> if the timeout elapsed before termination |
140 |
|
* @throws InterruptedException if interrupted while waiting |
141 |
|
*/ |
142 |
|
boolean awaitTermination(long timeout, TimeUnit unit) |
147 |
|
* Submits a value-returning task for execution and returns a |
148 |
|
* Future representing the pending results of the task. The |
149 |
|
* Future's <tt>get</tt> method will return the task's result upon |
150 |
< |
* <em>successful</em> completion. |
150 |
> |
* successful completion. |
151 |
|
* |
152 |
|
* <p> |
153 |
|
* If you would like to immediately block waiting |
161 |
|
* |
162 |
|
* @param task the task to submit |
163 |
|
* @return a Future representing pending completion of the task |
164 |
< |
* @throws RejectedExecutionException if task cannot be scheduled |
165 |
< |
* for execution |
166 |
< |
* @throws NullPointerException if task null |
164 |
> |
* @throws RejectedExecutionException if the task cannot be |
165 |
> |
* scheduled for execution |
166 |
> |
* @throws NullPointerException if the task is null |
167 |
|
*/ |
168 |
|
<T> Future<T> submit(Callable<T> task); |
169 |
|
|
175 |
|
* @param task the task to submit |
176 |
|
* @param result the result to return |
177 |
|
* @return a Future representing pending completion of the task |
178 |
< |
* @throws RejectedExecutionException if task cannot be scheduled |
179 |
< |
* for execution |
180 |
< |
* @throws NullPointerException if task null |
178 |
> |
* @throws RejectedExecutionException if the task cannot be |
179 |
> |
* scheduled for execution |
180 |
> |
* @throws NullPointerException if the task is null |
181 |
|
*/ |
182 |
|
<T> Future<T> submit(Runnable task, T result); |
183 |
|
|
184 |
|
/** |
185 |
|
* Submits a Runnable task for execution and returns a Future |
186 |
|
* representing that task. The Future's <tt>get</tt> method will |
187 |
< |
* return <tt>null</tt> upon successful completion. |
187 |
> |
* return <tt>null</tt> upon <em>successful</em> completion. |
188 |
|
* |
189 |
|
* @param task the task to submit |
190 |
|
* @return a Future representing pending completion of the task |
191 |
< |
* @throws RejectedExecutionException if task cannot be scheduled |
192 |
< |
* for execution |
193 |
< |
* @throws NullPointerException if task null |
191 |
> |
* @throws RejectedExecutionException if the task cannot be |
192 |
> |
* scheduled for execution |
193 |
> |
* @throws NullPointerException if the task is null |
194 |
|
*/ |
195 |
|
Future<?> submit(Runnable task); |
196 |
|
|
203 |
|
* terminated either normally or by throwing an exception. |
204 |
|
* The results of this method are undefined if the given |
205 |
|
* collection is modified while this operation is in progress. |
206 |
+ |
* |
207 |
|
* @param tasks the collection of tasks |
208 |
|
* @return A list of Futures representing the tasks, in the same |
209 |
< |
* sequential order as produced by the iterator for the given task |
210 |
< |
* list, each of which has completed. |
209 |
> |
* sequential order as produced by the iterator for the |
210 |
> |
* given task list, each of which has completed. |
211 |
|
* @throws InterruptedException if interrupted while waiting, in |
212 |
< |
* which case unfinished tasks are cancelled. |
212 |
> |
* which case unfinished tasks are cancelled. |
213 |
|
* @throws NullPointerException if tasks or any of its elements are <tt>null</tt> |
214 |
< |
* @throws RejectedExecutionException if any task cannot be scheduled |
215 |
< |
* for execution |
214 |
> |
* @throws RejectedExecutionException if any task cannot be |
215 |
> |
* scheduled for execution |
216 |
|
*/ |
217 |
|
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
218 |
|
throws InterruptedException; |
228 |
|
* terminated either normally or by throwing an exception. |
229 |
|
* The results of this method are undefined if the given |
230 |
|
* collection is modified while this operation is in progress. |
231 |
+ |
* |
232 |
|
* @param tasks the collection of tasks |
233 |
|
* @param timeout the maximum time to wait |
234 |
|
* @param unit the time unit of the timeout argument |
235 |
< |
* @return A list of Futures representing the tasks, in the same |
236 |
< |
* sequential order as produced by the iterator for the given |
237 |
< |
* task list. If the operation did not time out, each task will |
238 |
< |
* have completed. If it did time out, some of these tasks will |
239 |
< |
* not have completed. |
235 |
> |
* @return a list of Futures representing the tasks, in the same |
236 |
> |
* sequential order as produced by the iterator for the |
237 |
> |
* given task list. If the operation did not time out, |
238 |
> |
* each task will have completed. If it did time out, some |
239 |
> |
* of these tasks will not have completed. |
240 |
|
* @throws InterruptedException if interrupted while waiting, in |
241 |
< |
* which case unfinished tasks are cancelled. |
241 |
> |
* which case unfinished tasks are cancelled |
242 |
|
* @throws NullPointerException if tasks, any of its elements, or |
243 |
< |
* unit are <tt>null</tt> |
243 |
> |
* unit are <tt>null</tt> |
244 |
|
* @throws RejectedExecutionException if any task cannot be scheduled |
245 |
< |
* for execution |
245 |
> |
* for execution |
246 |
|
*/ |
247 |
|
<T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, |
248 |
|
long timeout, TimeUnit unit) |
255 |
|
* tasks that have not completed are cancelled. |
256 |
|
* The results of this method are undefined if the given |
257 |
|
* collection is modified while this operation is in progress. |
258 |
+ |
* |
259 |
|
* @param tasks the collection of tasks |
260 |
< |
* @return The result returned by one of the tasks. |
260 |
> |
* @return the result returned by one of the tasks |
261 |
|
* @throws InterruptedException if interrupted while waiting |
262 |
|
* @throws NullPointerException if tasks or any of its elements |
263 |
< |
* are <tt>null</tt> |
264 |
< |
* @throws IllegalArgumentException if tasks empty |
263 |
> |
* are <tt>null</tt> |
264 |
> |
* @throws IllegalArgumentException if tasks is empty |
265 |
|
* @throws ExecutionException if no task successfully completes |
266 |
|
* @throws RejectedExecutionException if tasks cannot be scheduled |
267 |
< |
* for execution |
267 |
> |
* for execution |
268 |
|
*/ |
269 |
|
<T> T invokeAny(Collection<Callable<T>> tasks) |
270 |
|
throws InterruptedException, ExecutionException; |
277 |
|
* completed are cancelled. |
278 |
|
* The results of this method are undefined if the given |
279 |
|
* collection is modified while this operation is in progress. |
280 |
+ |
* |
281 |
|
* @param tasks the collection of tasks |
282 |
|
* @param timeout the maximum time to wait |
283 |
|
* @param unit the time unit of the timeout argument |
284 |
< |
* @return The result returned by one of the tasks. |
284 |
> |
* @return the result returned by one of the tasks. |
285 |
|
* @throws InterruptedException if interrupted while waiting |
286 |
|
* @throws NullPointerException if tasks, any of its elements, or |
287 |
< |
* unit are <tt>null</tt> |
287 |
> |
* unit are <tt>null</tt> |
288 |
|
* @throws TimeoutException if the given timeout elapses before |
289 |
< |
* any task successfully completes |
289 |
> |
* any task successfully completes |
290 |
|
* @throws ExecutionException if no task successfully completes |
291 |
|
* @throws RejectedExecutionException if tasks cannot be scheduled |
292 |
< |
* for execution |
292 |
> |
* for execution |
293 |
|
*/ |
294 |
|
<T> T invokeAny(Collection<Callable<T>> tasks, |
295 |
|
long timeout, TimeUnit unit) |
296 |
|
throws InterruptedException, ExecutionException, TimeoutException; |
288 |
– |
|
297 |
|
} |