author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 42322 | c3474fef4fe4 |
child 45434 | 4582657c7260 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7518
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
38 |
/** |
|
39 |
* A {@link CompletionService} that uses a supplied {@link Executor} |
|
40 |
* to execute tasks. This class arranges that submitted tasks are, |
|
41 |
* upon completion, placed on a queue accessible using {@code take}. |
|
42 |
* The class is lightweight enough to be suitable for transient use |
|
43 |
* when processing groups of tasks. |
|
44 |
* |
|
45 |
* <p> |
|
46 |
* |
|
47 |
* <b>Usage Examples.</b> |
|
48 |
* |
|
49 |
* Suppose you have a set of solvers for a certain problem, each |
|
50 |
* returning a value of some type {@code Result}, and would like to |
|
51 |
* run them concurrently, processing the results of each of them that |
|
52 |
* return a non-null value, in some method {@code use(Result r)}. You |
|
53 |
* could write this as: |
|
54 |
* |
|
55 |
* <pre> {@code |
|
56 |
* void solve(Executor e, |
|
57 |
* Collection<Callable<Result>> solvers) |
|
58 |
* throws InterruptedException, ExecutionException { |
|
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
59 |
* CompletionService<Result> cs |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
60 |
* = new ExecutorCompletionService<>(e); |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
61 |
* solvers.forEach(cs::submit); |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
62 |
* for (int i = solvers.size(); i > 0; i--) { |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
63 |
* Result r = cs.take().get(); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
64 |
* if (r != null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
65 |
* use(r); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
66 |
* } |
2 | 67 |
* }}</pre> |
68 |
* |
|
69 |
* Suppose instead that you would like to use the first non-null result |
|
70 |
* of the set of tasks, ignoring any that encounter exceptions, |
|
71 |
* and cancelling all other tasks when the first one is ready: |
|
72 |
* |
|
73 |
* <pre> {@code |
|
74 |
* void solve(Executor e, |
|
75 |
* Collection<Callable<Result>> solvers) |
|
76 |
* throws InterruptedException { |
|
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
77 |
* CompletionService<Result> cs |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
78 |
* = new ExecutorCompletionService<>(e); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
79 |
* int n = solvers.size(); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
80 |
* List<Future<Result>> futures = new ArrayList<>(n); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
81 |
* Result result = null; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
82 |
* try { |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
38551
diff
changeset
|
83 |
* solvers.forEach(solver -> futures.add(cs.submit(solver))); |
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
84 |
* for (int i = n; i > 0; i--) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
85 |
* try { |
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
86 |
* Result r = cs.take().get(); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
87 |
* if (r != null) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
88 |
* result = r; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
89 |
* break; |
2 | 90 |
* } |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
91 |
* } catch (ExecutionException ignore) {} |
2 | 92 |
* } |
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
32991
diff
changeset
|
93 |
* } finally { |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
38551
diff
changeset
|
94 |
* futures.forEach(future -> future.cancel(true)); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
95 |
* } |
2 | 96 |
* |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
97 |
* if (result != null) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
98 |
* use(result); |
2 | 99 |
* }}</pre> |
100 |
*/ |
|
101 |
public class ExecutorCompletionService<V> implements CompletionService<V> { |
|
102 |
private final Executor executor; |
|
103 |
private final AbstractExecutorService aes; |
|
104 |
private final BlockingQueue<Future<V>> completionQueue; |
|
105 |
||
106 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
107 |
* FutureTask extension to enqueue upon completion. |
2 | 108 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
109 |
private static class QueueingFuture<V> extends FutureTask<Void> { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
110 |
QueueingFuture(RunnableFuture<V> task, |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
111 |
BlockingQueue<Future<V>> completionQueue) { |
2 | 112 |
super(task, null); |
113 |
this.task = task; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
114 |
this.completionQueue = completionQueue; |
2 | 115 |
} |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
116 |
private final Future<V> task; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
117 |
private final BlockingQueue<Future<V>> completionQueue; |
2 | 118 |
protected void done() { completionQueue.add(task); } |
119 |
} |
|
120 |
||
121 |
private RunnableFuture<V> newTaskFor(Callable<V> task) { |
|
122 |
if (aes == null) |
|
123 |
return new FutureTask<V>(task); |
|
124 |
else |
|
125 |
return aes.newTaskFor(task); |
|
126 |
} |
|
127 |
||
128 |
private RunnableFuture<V> newTaskFor(Runnable task, V result) { |
|
129 |
if (aes == null) |
|
130 |
return new FutureTask<V>(task, result); |
|
131 |
else |
|
132 |
return aes.newTaskFor(task, result); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Creates an ExecutorCompletionService using the supplied |
|
137 |
* executor for base task execution and a |
|
138 |
* {@link LinkedBlockingQueue} as a completion queue. |
|
139 |
* |
|
140 |
* @param executor the executor to use |
|
141 |
* @throws NullPointerException if executor is {@code null} |
|
142 |
*/ |
|
143 |
public ExecutorCompletionService(Executor executor) { |
|
144 |
if (executor == null) |
|
145 |
throw new NullPointerException(); |
|
146 |
this.executor = executor; |
|
147 |
this.aes = (executor instanceof AbstractExecutorService) ? |
|
148 |
(AbstractExecutorService) executor : null; |
|
149 |
this.completionQueue = new LinkedBlockingQueue<Future<V>>(); |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Creates an ExecutorCompletionService using the supplied |
|
154 |
* executor for base task execution and the supplied queue as its |
|
155 |
* completion queue. |
|
156 |
* |
|
157 |
* @param executor the executor to use |
|
158 |
* @param completionQueue the queue to use as the completion queue |
|
159 |
* normally one dedicated for use by this service. This |
|
160 |
* queue is treated as unbounded -- failed attempted |
|
21278 | 161 |
* {@code Queue.add} operations for completed tasks cause |
2 | 162 |
* them not to be retrievable. |
163 |
* @throws NullPointerException if executor or completionQueue are {@code null} |
|
164 |
*/ |
|
165 |
public ExecutorCompletionService(Executor executor, |
|
166 |
BlockingQueue<Future<V>> completionQueue) { |
|
167 |
if (executor == null || completionQueue == null) |
|
168 |
throw new NullPointerException(); |
|
169 |
this.executor = executor; |
|
170 |
this.aes = (executor instanceof AbstractExecutorService) ? |
|
171 |
(AbstractExecutorService) executor : null; |
|
172 |
this.completionQueue = completionQueue; |
|
173 |
} |
|
174 |
||
175 |
public Future<V> submit(Callable<V> task) { |
|
176 |
if (task == null) throw new NullPointerException(); |
|
177 |
RunnableFuture<V> f = newTaskFor(task); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
178 |
executor.execute(new QueueingFuture<V>(f, completionQueue)); |
2 | 179 |
return f; |
180 |
} |
|
181 |
||
182 |
public Future<V> submit(Runnable task, V result) { |
|
183 |
if (task == null) throw new NullPointerException(); |
|
184 |
RunnableFuture<V> f = newTaskFor(task, result); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
185 |
executor.execute(new QueueingFuture<V>(f, completionQueue)); |
2 | 186 |
return f; |
187 |
} |
|
188 |
||
189 |
public Future<V> take() throws InterruptedException { |
|
190 |
return completionQueue.take(); |
|
191 |
} |
|
192 |
||
193 |
public Future<V> poll() { |
|
194 |
return completionQueue.poll(); |
|
195 |
} |
|
196 |
||
7518 | 197 |
public Future<V> poll(long timeout, TimeUnit unit) |
198 |
throws InterruptedException { |
|
2 | 199 |
return completionQueue.poll(timeout, unit); |
200 |
} |
|
201 |
||
202 |
} |