ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ExecutorCompletionService.java
Revision: 1.3
Committed: Mon Dec 15 12:06:00 2003 UTC (20 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.2: +25 -16 lines
Log Message:
Improved documentation; changed constructors and accesors

File Contents

# Content
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
9
10 /**
11 * A {@link CompletionService} that uses a supplied {@link Executor}
12 * to execute tasks.
13 */
14 public class ExecutorCompletionService<V> implements CompletionService<V> {
15 private final Executor executor;
16 private final BlockingQueue<Future<V>> completionQueue;
17
18 /**
19 * FutureTask extension to enqueue upon completion
20 */
21 private class QueueingFuture<T> extends FutureTask<T> {
22 QueueingFuture(Callable<T> c) { super(c); }
23 QueueingFuture(Runnable t, T r) { super(t, r); }
24 protected void done() { completionQueue.add((Future<V>)this); }
25 }
26
27 /**
28 * Creates an ExecutorCompletionService using the supplied
29 * executor for base task execution and a
30 * {@link LinkedBlockingQueue} as a completion queue.
31 * @param executor the executor to use; normally
32 * one dedicated for use by this service
33 8 @throws NullPointerException if executor is <tt>null</tt>
34 */
35 public ExecutorCompletionService(Executor executor) {
36 if (executor == null)
37 throw new NullPointerException();
38 this.executor = executor;
39 this.completionQueue = new LinkedBlockingQueue<Future<V>>();
40 }
41
42 /**
43 * Creates an ExecutorCompletionService using the supplied
44 * executor for base task execution and the supplied queue as its
45 * completion queue.
46 * @param executor the executor to use; normally
47 * one dedicated for use by this service
48 * @param completionQueue the queue to use as the completion queue;
49 * normally one dedicated for use by this service
50 8 @throws NullPointerException if executor or completionQueue are <tt>null</tt>
51 */
52 public ExecutorCompletionService(Executor executor,
53 BlockingQueue<Future<V>> completionQueue) {
54 if (executor == null || completionQueue == null)
55 throw new NullPointerException();
56 this.executor = executor;
57 this.completionQueue = completionQueue;
58 }
59
60 public Future<V> submit(Callable<V> task) {
61 QueueingFuture<V> f = new QueueingFuture<V>(task);
62 executor.execute(f);
63 return f;
64 }
65
66 public Future<V> submit(Runnable task, V result) {
67 QueueingFuture<V> f = new QueueingFuture<V>(task, result);
68 executor.execute(f);
69 return f;
70 }
71
72 public Future<V> take() throws InterruptedException {
73 return completionQueue.take();
74 }
75
76 public Future<V> poll() {
77 return completionQueue.poll();
78 }
79
80 public Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException {
81 return completionQueue.poll(timeout, unit);
82 }
83 }
84
85