254 |
|
return new PrivilegedThreadFactory(); |
255 |
|
} |
256 |
|
|
257 |
+ |
|
258 |
+ |
/** |
259 |
+ |
* Creates and returns a {@link Callable} object that, when |
260 |
+ |
* called, runs the given task and returns the given result. This |
261 |
+ |
* can be useful when applying methods requiring a |
262 |
+ |
* <tt>Callable</tt> to an otherwise resultless action. |
263 |
+ |
* @param task the task to run |
264 |
+ |
* @param result the result to return |
265 |
+ |
*/ |
266 |
+ |
public static <T> Callable<T> callable(Runnable task, T result) { |
267 |
+ |
return new RunnableAdapter<T>(task, result); |
268 |
+ |
} |
269 |
+ |
|
270 |
+ |
/** |
271 |
+ |
* Creates and returns a {@link Callable} object that, when |
272 |
+ |
* called, runs the given task and returns <tt>null</tt> |
273 |
+ |
* @param task the task to run |
274 |
+ |
*/ |
275 |
+ |
public static Callable<Object> callable(Runnable task) { |
276 |
+ |
return new RunnableAdapter<Object>(task, null); |
277 |
+ |
} |
278 |
+ |
|
279 |
+ |
/** |
280 |
+ |
* Creates and returns a {@link Callable} object that, when |
281 |
+ |
* called, runs the given privileged action and returns its result |
282 |
+ |
* @param action the privileged action to run |
283 |
+ |
*/ |
284 |
+ |
public static Callable<Object> callable(PrivilegedAction action) { |
285 |
+ |
return new PrivilegedActionAdapter(action); |
286 |
+ |
} |
287 |
+ |
|
288 |
+ |
/** |
289 |
+ |
* Creates and returns a {@link Callable} object that, when |
290 |
+ |
* called, runs the given privileged exception action and returns its result |
291 |
+ |
* @param action the privileged exception action to run |
292 |
+ |
*/ |
293 |
+ |
public static Callable<Object> callable(PrivilegedExceptionAction action) { |
294 |
+ |
return new PrivilegedExceptionActionAdapter(action); |
295 |
+ |
} |
296 |
+ |
|
297 |
+ |
/** |
298 |
+ |
* A callable that runs given task and returns given result |
299 |
+ |
*/ |
300 |
+ |
static class RunnableAdapter<T> implements Callable<T> { |
301 |
+ |
private final Runnable task; |
302 |
+ |
private final T result; |
303 |
+ |
RunnableAdapter(Runnable task, T result) { |
304 |
+ |
this.task = task; |
305 |
+ |
this.result = result; |
306 |
+ |
} |
307 |
+ |
public T call() { |
308 |
+ |
task.run(); |
309 |
+ |
return result; |
310 |
+ |
} |
311 |
+ |
} |
312 |
+ |
|
313 |
+ |
/** |
314 |
+ |
* A callable that runs given privileged action and returns its result |
315 |
+ |
*/ |
316 |
+ |
static class PrivilegedActionAdapter implements Callable<Object> { |
317 |
+ |
PrivilegedActionAdapter(PrivilegedAction action) { |
318 |
+ |
this.action = action; |
319 |
+ |
} |
320 |
+ |
public Object call () { |
321 |
+ |
return action.run(); |
322 |
+ |
} |
323 |
+ |
private final PrivilegedAction action; |
324 |
+ |
} |
325 |
+ |
|
326 |
+ |
/** |
327 |
+ |
* A callable that runs given privileged exception action and returns its result |
328 |
+ |
*/ |
329 |
+ |
static class PrivilegedExceptionActionAdapter implements Callable<Object> { |
330 |
+ |
PrivilegedExceptionActionAdapter(PrivilegedExceptionAction action) { |
331 |
+ |
this.action = action; |
332 |
+ |
} |
333 |
+ |
public Object call () throws Exception { |
334 |
+ |
return action.run(); |
335 |
+ |
} |
336 |
+ |
private final PrivilegedExceptionAction action; |
337 |
+ |
} |
338 |
+ |
|
339 |
|
static class DefaultThreadFactory implements ThreadFactory { |
340 |
|
static final AtomicInteger poolNumber = new AtomicInteger(1); |
341 |
|
final ThreadGroup group; |
390 |
|
|
391 |
|
} |
392 |
|
|
311 |
– |
|
393 |
|
/** |
394 |
|
* A wrapper class that exposes only the ExecutorService methods |
395 |
|
* of an implementation. |
406 |
|
throws InterruptedException { |
407 |
|
return e.awaitTermination(timeout, unit); |
408 |
|
} |
409 |
< |
public <T> Future<T> submit(Runnable task, T result) { |
410 |
< |
return e.submit(task, result); |
409 |
> |
public Future<?> submit(Runnable task) { |
410 |
> |
return e.submit(task); |
411 |
|
} |
412 |
|
public <T> Future<T> submit(Callable<T> task) { |
413 |
|
return e.submit(task); |
414 |
|
} |
415 |
|
|
335 |
– |
public void invoke(Runnable task) throws ExecutionException, InterruptedException { |
336 |
– |
e.invoke(task); |
337 |
– |
} |
416 |
|
public <T> T invoke(Callable<T> task) throws ExecutionException, InterruptedException { |
417 |
|
return e.invoke(task); |
418 |
|
} |
341 |
– |
public Future<Object> submit(PrivilegedAction action) { |
342 |
– |
return e.submit(action); |
343 |
– |
} |
344 |
– |
public Future<Object> submit(PrivilegedExceptionAction action) { |
345 |
– |
return e.submit(action); |
346 |
– |
} |
347 |
– |
public <T> List<Future<T>> invokeAll(Collection<Runnable> tasks, T result) |
348 |
– |
throws InterruptedException { |
349 |
– |
return e.invokeAll(tasks, result); |
350 |
– |
} |
351 |
– |
public <T> List<Future<T>> invokeAll(Collection<Runnable> tasks, T result, |
352 |
– |
long timeout, TimeUnit unit) |
353 |
– |
throws InterruptedException { |
354 |
– |
return e.invokeAll(tasks, result, timeout, unit); |
355 |
– |
} |
419 |
|
public <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks) |
420 |
|
throws InterruptedException { |
421 |
|
return e.invokeAll(tasks); |
434 |
|
throws InterruptedException, ExecutionException, TimeoutException { |
435 |
|
return e.invokeAny(tasks, timeout, unit); |
436 |
|
} |
374 |
– |
public <T> T invokeAny(Collection<Runnable> tasks, T result) |
375 |
– |
throws InterruptedException, ExecutionException { |
376 |
– |
return e.invokeAny(tasks, result); |
377 |
– |
} |
378 |
– |
public <T> T invokeAny(Collection<Runnable> tasks, T result, |
379 |
– |
long timeout, TimeUnit unit) |
380 |
– |
throws InterruptedException, ExecutionException, TimeoutException { |
381 |
– |
return e.invokeAny(tasks, result, timeout, unit); |
382 |
– |
} |
437 |
|
} |
438 |
|
|
439 |
|
/** |