1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
28 |
|
static class DirectExecutorService extends AbstractExecutorService { |
29 |
|
public void execute(Runnable r) { r.run(); } |
30 |
|
public void shutdown() { shutdown = true; } |
31 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
31 |
> |
public List<Runnable> shutdownNow() { |
32 |
> |
shutdown = true; |
33 |
> |
return Collections.EMPTY_LIST; |
34 |
> |
} |
35 |
|
public boolean isShutdown() { return shutdown; } |
36 |
|
public boolean isTerminated() { return isShutdown(); } |
37 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
37 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
38 |
> |
return isShutdown(); |
39 |
> |
} |
40 |
|
private volatile boolean shutdown = false; |
41 |
|
} |
42 |
|
|
52 |
|
assertTrue(task.done); |
53 |
|
} |
54 |
|
|
51 |
– |
|
55 |
|
/** |
56 |
|
* Completed submit(callable) returns result |
57 |
|
*/ |
82 |
|
assertSame(TEST_STRING, result); |
83 |
|
} |
84 |
|
|
82 |
– |
|
85 |
|
/** |
86 |
|
* A submitted privileged action runs to completion |
87 |
|
*/ |
154 |
|
} catch (NullPointerException success) {} |
155 |
|
} |
156 |
|
|
155 |
– |
|
157 |
|
/** |
158 |
|
* submit(null callable) throws NPE |
159 |
|
*/ |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* submit(runnable) throws RejectedExecutionException if |
169 |
< |
* executor is saturated. |
169 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
170 |
|
*/ |
171 |
< |
public void testExecute1() { |
172 |
< |
ThreadPoolExecutor p = |
173 |
< |
new ThreadPoolExecutor(1, 1, |
174 |
< |
60, TimeUnit.SECONDS, |
175 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
176 |
< |
try { |
177 |
< |
for (int i = 0; i < 2; ++i) |
178 |
< |
p.submit(new MediumRunnable()); |
179 |
< |
for (int i = 0; i < 2; ++i) { |
180 |
< |
try { |
181 |
< |
p.submit(new MediumRunnable()); |
182 |
< |
shouldThrow(); |
183 |
< |
} catch (RejectedExecutionException success) {} |
184 |
< |
} |
185 |
< |
} finally { |
186 |
< |
joinPool(p); |
187 |
< |
} |
188 |
< |
} |
189 |
< |
|
190 |
< |
/** |
191 |
< |
* submit(callable) throws RejectedExecutionException |
192 |
< |
* if executor is saturated. |
193 |
< |
*/ |
194 |
< |
public void testExecute2() { |
195 |
< |
ThreadPoolExecutor p = |
196 |
< |
new ThreadPoolExecutor(1, 1, |
197 |
< |
60, TimeUnit.SECONDS, |
198 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
171 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
172 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
173 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
174 |
> |
final ExecutorService p |
175 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
176 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
177 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
178 |
> |
public Void realCall() throws InterruptedException { |
179 |
> |
quittingTime.await(); |
180 |
> |
return null; |
181 |
> |
}}; |
182 |
|
try { |
183 |
< |
for (int i = 0; i < 2; ++i) |
184 |
< |
p.submit(new MediumRunnable()); |
185 |
< |
for (int i = 0; i < 2; ++i) { |
186 |
< |
try { |
187 |
< |
p.submit(new SmallCallable()); |
188 |
< |
shouldThrow(); |
189 |
< |
} catch (RejectedExecutionException success) {} |
190 |
< |
} |
183 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
184 |
> |
public void realRun() throws Exception { |
185 |
> |
Future<Void> future = p.submit(awaiter); |
186 |
> |
submitted.countDown(); |
187 |
> |
future.get(); |
188 |
> |
}}); |
189 |
> |
t.start(); |
190 |
> |
submitted.await(); |
191 |
> |
t.interrupt(); |
192 |
> |
t.join(); |
193 |
|
} finally { |
194 |
+ |
quittingTime.countDown(); |
195 |
|
joinPool(p); |
196 |
|
} |
197 |
|
} |
198 |
|
|
213 |
– |
|
214 |
– |
/** |
215 |
– |
* Blocking on submit(callable) throws InterruptedException if |
216 |
– |
* caller interrupted. |
217 |
– |
*/ |
218 |
– |
public void testInterruptedSubmit() throws InterruptedException { |
219 |
– |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
220 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
221 |
– |
public void realRun() throws Exception { |
222 |
– |
p.submit(new CheckedCallable<Object>() { |
223 |
– |
public Object realCall() |
224 |
– |
throws InterruptedException { |
225 |
– |
Thread.sleep(SMALL_DELAY_MS); |
226 |
– |
return null; |
227 |
– |
}}).get(); |
228 |
– |
}}); |
229 |
– |
|
230 |
– |
t.start(); |
231 |
– |
Thread.sleep(SHORT_DELAY_MS); |
232 |
– |
t.interrupt(); |
233 |
– |
joinPool(p); |
234 |
– |
} |
235 |
– |
|
199 |
|
/** |
200 |
< |
* get of submitted callable throws InterruptedException if callable |
201 |
< |
* interrupted |
239 |
< |
*/ |
240 |
< |
public void testSubmitIE() throws InterruptedException { |
241 |
< |
final ThreadPoolExecutor p = |
242 |
< |
new ThreadPoolExecutor(1, 1, |
243 |
< |
60, TimeUnit.SECONDS, |
244 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
245 |
< |
|
246 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
247 |
< |
public void realRun() throws Exception { |
248 |
< |
p.submit(new SmallCallable()).get(); |
249 |
< |
}}); |
250 |
< |
|
251 |
< |
t.start(); |
252 |
< |
Thread.sleep(SHORT_DELAY_MS); |
253 |
< |
t.interrupt(); |
254 |
< |
t.join(); |
255 |
< |
joinPool(p); |
256 |
< |
} |
257 |
< |
|
258 |
< |
/** |
259 |
< |
* get of submit(callable) throws ExecutionException if callable |
260 |
< |
* throws exception |
200 |
> |
* get of submit(callable) throws ExecutionException if callable |
201 |
> |
* throws exception |
202 |
|
*/ |
203 |
|
public void testSubmitEE() throws InterruptedException { |
204 |
|
ThreadPoolExecutor p = |
221 |
|
/** |
222 |
|
* invokeAny(null) throws NPE |
223 |
|
*/ |
224 |
< |
public void testInvokeAny1() |
284 |
< |
throws InterruptedException, ExecutionException { |
224 |
> |
public void testInvokeAny1() throws Exception { |
225 |
|
ExecutorService e = new DirectExecutorService(); |
226 |
|
try { |
227 |
|
e.invokeAny(null); |
235 |
|
/** |
236 |
|
* invokeAny(empty collection) throws IAE |
237 |
|
*/ |
238 |
< |
public void testInvokeAny2() |
299 |
< |
throws InterruptedException, ExecutionException { |
238 |
> |
public void testInvokeAny2() throws Exception { |
239 |
|
ExecutorService e = new DirectExecutorService(); |
240 |
|
try { |
241 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
380 |
|
} |
381 |
|
} |
382 |
|
|
444 |
– |
|
383 |
|
/** |
384 |
|
* timed invokeAny(null) throws NPE |
385 |
|
*/ |
584 |
|
try { |
585 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
586 |
|
l.add(new StringTask()); |
587 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
587 |
> |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
588 |
|
l.add(new StringTask()); |
589 |
|
List<Future<String>> futures = |
590 |
< |
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
590 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
591 |
|
assertEquals(3, futures.size()); |
592 |
|
Iterator<Future<String>> it = futures.iterator(); |
593 |
|
Future<String> f1 = it.next(); |
596 |
|
assertTrue(f1.isDone()); |
597 |
|
assertFalse(f1.isCancelled()); |
598 |
|
assertTrue(f2.isDone()); |
599 |
+ |
assertFalse(f2.isCancelled()); |
600 |
|
assertTrue(f3.isDone()); |
601 |
|
assertTrue(f3.isCancelled()); |
602 |
|
} finally { |