1 7 8 package java.util.concurrent; 9 10 11 77 public class ExecutorCompletionService<V> implements CompletionService <V> { 78 private final Executor executor; 79 private final BlockingQueue <Future <V>> completionQueue; 80 81 84 private class QueueingFuture extends FutureTask <V> { 85 QueueingFuture(Callable <V> c) { super(c); } 86 QueueingFuture(Runnable t, V r) { super(t, r); } 87 protected void done() { completionQueue.add(this); } 88 } 89 90 97 public ExecutorCompletionService(Executor executor) { 98 if (executor == null) 99 throw new NullPointerException (); 100 this.executor = executor; 101 this.completionQueue = new LinkedBlockingQueue <Future <V>>(); 102 } 103 104 113 public ExecutorCompletionService(Executor executor, 114 BlockingQueue <Future <V>> completionQueue) { 115 if (executor == null || completionQueue == null) 116 throw new NullPointerException (); 117 this.executor = executor; 118 this.completionQueue = completionQueue; 119 } 120 121 public Future <V> submit(Callable <V> task) { 122 if (task == null) throw new NullPointerException (); 123 QueueingFuture f = new QueueingFuture(task); 124 executor.execute(f); 125 return f; 126 } 127 128 public Future <V> submit(Runnable task, V result) { 129 if (task == null) throw new NullPointerException (); 130 QueueingFuture f = new QueueingFuture(task, result); 131 executor.execute(f); 132 return f; 133 } 134 135 public Future <V> take() throws InterruptedException { 136 return completionQueue.take(); 137 } 138 139 public Future <V> poll() { 140 return completionQueue.poll(); 141 } 142 143 public Future <V> poll(long timeout, TimeUnit unit) throws InterruptedException { 144 return completionQueue.poll(timeout, unit); 145 } 146 147 } 148 149 150 | Popular Tags |