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 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.atomic.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
> |
|
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.HashSet; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.Callable; |
17 |
> |
import java.util.concurrent.CancellationException; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.ExecutionException; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.Future; |
23 |
> |
import java.util.concurrent.RejectedExecutionException; |
24 |
> |
import java.util.concurrent.ScheduledFuture; |
25 |
> |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
> |
import java.util.concurrent.ThreadFactory; |
27 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
28 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
29 |
> |
|
30 |
> |
import junit.framework.Test; |
31 |
> |
import junit.framework.TestSuite; |
32 |
|
|
33 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
34 |
|
public static void main(String[] args) { |
35 |
< |
junit.textui.TestRunner.run (suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
public static Test suite() { |
38 |
< |
return new TestSuite(ScheduledExecutorTest.class); |
38 |
> |
return new TestSuite(ScheduledExecutorTest.class); |
39 |
|
} |
40 |
|
|
23 |
– |
|
41 |
|
/** |
42 |
|
* execute successfully executes a runnable |
43 |
|
*/ |
44 |
|
public void testExecute() throws InterruptedException { |
45 |
< |
TrackedShortRunnable runnable =new TrackedShortRunnable(); |
46 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
47 |
< |
p1.execute(runnable); |
48 |
< |
assertFalse(runnable.done); |
49 |
< |
Thread.sleep(SHORT_DELAY_MS); |
50 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
51 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
52 |
< |
assertTrue(runnable.done); |
53 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
54 |
< |
joinPool(p1); |
45 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
46 |
> |
final CountDownLatch done = new CountDownLatch(1); |
47 |
> |
final Runnable task = new CheckedRunnable() { |
48 |
> |
public void realRun() { |
49 |
> |
done.countDown(); |
50 |
> |
}}; |
51 |
> |
try { |
52 |
> |
p.execute(task); |
53 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
54 |
> |
} finally { |
55 |
> |
joinPool(p); |
56 |
> |
} |
57 |
|
} |
58 |
|
|
40 |
– |
|
59 |
|
/** |
60 |
|
* delayed schedule of callable successfully executes after delay |
61 |
|
*/ |
62 |
|
public void testSchedule1() throws Exception { |
63 |
< |
TrackedCallable callable = new TrackedCallable(); |
64 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
65 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
66 |
< |
assertFalse(callable.done); |
67 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
68 |
< |
assertTrue(callable.done); |
69 |
< |
assertEquals(Boolean.TRUE, f.get()); |
70 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
71 |
< |
joinPool(p1); |
63 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
64 |
> |
final long startTime = System.nanoTime(); |
65 |
> |
final CountDownLatch done = new CountDownLatch(1); |
66 |
> |
try { |
67 |
> |
Callable task = new CheckedCallable<Boolean>() { |
68 |
> |
public Boolean realCall() { |
69 |
> |
done.countDown(); |
70 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
71 |
> |
return Boolean.TRUE; |
72 |
> |
}}; |
73 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
74 |
> |
assertSame(Boolean.TRUE, f.get()); |
75 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
76 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
77 |
> |
} finally { |
78 |
> |
joinPool(p); |
79 |
> |
} |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* delayed schedule of runnable successfully executes after delay |
84 |
< |
*/ |
85 |
< |
public void testSchedule3() throws InterruptedException { |
86 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
87 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
88 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
89 |
< |
Thread.sleep(SHORT_DELAY_MS); |
90 |
< |
assertFalse(runnable.done); |
91 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
92 |
< |
assertTrue(runnable.done); |
93 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
94 |
< |
joinPool(p1); |
83 |
> |
* delayed schedule of runnable successfully executes after delay |
84 |
> |
*/ |
85 |
> |
public void testSchedule3() throws Exception { |
86 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
87 |
> |
final long startTime = System.nanoTime(); |
88 |
> |
final CountDownLatch done = new CountDownLatch(1); |
89 |
> |
try { |
90 |
> |
Runnable task = new CheckedRunnable() { |
91 |
> |
public void realRun() { |
92 |
> |
done.countDown(); |
93 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
94 |
> |
}}; |
95 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
96 |
> |
await(done); |
97 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
98 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
99 |
> |
} finally { |
100 |
> |
joinPool(p); |
101 |
> |
} |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
106 |
|
*/ |
107 |
< |
public void testSchedule4() throws InterruptedException { |
108 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
109 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
110 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
111 |
< |
assertFalse(runnable.done); |
112 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
113 |
< |
assertTrue(runnable.done); |
114 |
< |
h.cancel(true); |
115 |
< |
joinPool(p1); |
116 |
< |
} |
117 |
< |
|
118 |
< |
static class RunnableCounter implements Runnable { |
119 |
< |
AtomicInteger count = new AtomicInteger(0); |
120 |
< |
public void run() { count.getAndIncrement(); } |
107 |
> |
public void testSchedule4() throws Exception { |
108 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
109 |
> |
final long startTime = System.nanoTime(); |
110 |
> |
final CountDownLatch done = new CountDownLatch(1); |
111 |
> |
try { |
112 |
> |
Runnable task = new CheckedRunnable() { |
113 |
> |
public void realRun() { |
114 |
> |
done.countDown(); |
115 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
116 |
> |
}}; |
117 |
> |
ScheduledFuture f = |
118 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
119 |
> |
LONG_DELAY_MS, MILLISECONDS); |
120 |
> |
await(done); |
121 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
122 |
> |
f.cancel(true); |
123 |
> |
} finally { |
124 |
> |
joinPool(p); |
125 |
> |
} |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
130 |
|
*/ |
131 |
< |
public void testSchedule5() throws InterruptedException { |
132 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
133 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
134 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
135 |
< |
assertFalse(runnable.done); |
136 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
137 |
< |
assertTrue(runnable.done); |
138 |
< |
h.cancel(true); |
139 |
< |
joinPool(p1); |
131 |
> |
public void testSchedule5() throws Exception { |
132 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
133 |
> |
final long startTime = System.nanoTime(); |
134 |
> |
final CountDownLatch done = new CountDownLatch(1); |
135 |
> |
try { |
136 |
> |
Runnable task = new CheckedRunnable() { |
137 |
> |
public void realRun() { |
138 |
> |
done.countDown(); |
139 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
140 |
> |
}}; |
141 |
> |
ScheduledFuture f = |
142 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
143 |
> |
LONG_DELAY_MS, MILLISECONDS); |
144 |
> |
await(done); |
145 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
146 |
> |
f.cancel(true); |
147 |
> |
} finally { |
148 |
> |
joinPool(p); |
149 |
> |
} |
150 |
> |
} |
151 |
> |
|
152 |
> |
static class RunnableCounter implements Runnable { |
153 |
> |
AtomicInteger count = new AtomicInteger(0); |
154 |
> |
public void run() { count.getAndIncrement(); } |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
159 |
|
*/ |
160 |
|
public void testFixedRateSequence() throws InterruptedException { |
161 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
162 |
< |
RunnableCounter counter = new RunnableCounter(); |
163 |
< |
ScheduledFuture h = |
164 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
165 |
< |
Thread.sleep(SMALL_DELAY_MS); |
166 |
< |
h.cancel(true); |
167 |
< |
int c = counter.count.get(); |
168 |
< |
// By time scaling conventions, we must have at least |
169 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
170 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
171 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
172 |
< |
joinPool(p1); |
161 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
162 |
> |
try { |
163 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
164 |
> |
long startTime = System.nanoTime(); |
165 |
> |
int cycles = 10; |
166 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
167 |
> |
Runnable task = new CheckedRunnable() { |
168 |
> |
public void realRun() { done.countDown(); }}; |
169 |
> |
ScheduledFuture h = |
170 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
171 |
> |
done.await(); |
172 |
> |
h.cancel(true); |
173 |
> |
double normalizedTime = |
174 |
> |
(double) millisElapsedSince(startTime) / delay; |
175 |
> |
if (normalizedTime >= cycles - 1 && |
176 |
> |
normalizedTime <= cycles) |
177 |
> |
return; |
178 |
> |
} |
179 |
> |
throw new AssertionError("unexpected execution rate"); |
180 |
> |
} finally { |
181 |
> |
joinPool(p); |
182 |
> |
} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
186 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
187 |
|
*/ |
188 |
|
public void testFixedDelaySequence() throws InterruptedException { |
189 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
190 |
< |
RunnableCounter counter = new RunnableCounter(); |
191 |
< |
ScheduledFuture h = |
192 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
193 |
< |
Thread.sleep(SMALL_DELAY_MS); |
194 |
< |
h.cancel(true); |
195 |
< |
int c = counter.count.get(); |
196 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
197 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
198 |
< |
joinPool(p1); |
189 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
190 |
> |
try { |
191 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
192 |
> |
long startTime = System.nanoTime(); |
193 |
> |
int cycles = 10; |
194 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
195 |
> |
Runnable task = new CheckedRunnable() { |
196 |
> |
public void realRun() { done.countDown(); }}; |
197 |
> |
ScheduledFuture h = |
198 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
199 |
> |
done.await(); |
200 |
> |
h.cancel(true); |
201 |
> |
double normalizedTime = |
202 |
> |
(double) millisElapsedSince(startTime) / delay; |
203 |
> |
if (normalizedTime >= cycles - 1 && |
204 |
> |
normalizedTime <= cycles) |
205 |
> |
return; |
206 |
> |
} |
207 |
> |
throw new AssertionError("unexpected execution rate"); |
208 |
> |
} finally { |
209 |
> |
joinPool(p); |
210 |
> |
} |
211 |
|
} |
212 |
|
|
138 |
– |
|
213 |
|
/** |
214 |
< |
* execute (null) throws NPE |
214 |
> |
* execute(null) throws NPE |
215 |
|
*/ |
216 |
|
public void testExecuteNull() throws InterruptedException { |
217 |
|
ScheduledThreadPoolExecutor se = null; |
218 |
|
try { |
219 |
< |
se = new ScheduledThreadPoolExecutor(1); |
220 |
< |
se.execute(null); |
219 |
> |
se = new ScheduledThreadPoolExecutor(1); |
220 |
> |
se.execute(null); |
221 |
|
shouldThrow(); |
222 |
< |
} catch (NullPointerException success) {} |
222 |
> |
} catch (NullPointerException success) {} |
223 |
|
|
224 |
< |
joinPool(se); |
224 |
> |
joinPool(se); |
225 |
|
} |
226 |
|
|
227 |
|
/** |
228 |
< |
* schedule (null) throws NPE |
228 |
> |
* schedule(null) throws NPE |
229 |
|
*/ |
230 |
|
public void testScheduleNull() throws InterruptedException { |
231 |
|
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
232 |
< |
try { |
232 |
> |
try { |
233 |
|
TrackedCallable callable = null; |
234 |
< |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
234 |
> |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
235 |
|
shouldThrow(); |
236 |
< |
} catch (NullPointerException success) {} |
237 |
< |
joinPool(se); |
236 |
> |
} catch (NullPointerException success) {} |
237 |
> |
joinPool(se); |
238 |
|
} |
239 |
|
|
240 |
|
/** |
252 |
|
} |
253 |
|
|
254 |
|
joinPool(se); |
181 |
– |
|
255 |
|
} |
256 |
|
|
257 |
|
/** |
273 |
|
/** |
274 |
|
* schedule callable throws RejectedExecutionException if shutdown |
275 |
|
*/ |
276 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
277 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
278 |
< |
try { |
276 |
> |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
277 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
278 |
> |
try { |
279 |
|
se.shutdown(); |
280 |
|
se.schedule(new NoOpCallable(), |
281 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
283 |
|
} catch (RejectedExecutionException success) { |
284 |
|
} catch (SecurityException ok) { |
285 |
|
} |
286 |
< |
joinPool(se); |
286 |
> |
joinPool(se); |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
290 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
291 |
|
*/ |
292 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
293 |
|
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
< |
* getActiveCount increases but doesn't overestimate, when a |
323 |
< |
* thread becomes active |
322 |
> |
* getActiveCount increases but doesn't overestimate, when a |
323 |
> |
* thread becomes active |
324 |
|
*/ |
325 |
|
public void testGetActiveCount() throws InterruptedException { |
326 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
327 |
< |
assertEquals(0, p2.getActiveCount()); |
328 |
< |
p2.execute(new SmallRunnable()); |
329 |
< |
Thread.sleep(SHORT_DELAY_MS); |
330 |
< |
assertEquals(1, p2.getActiveCount()); |
331 |
< |
joinPool(p2); |
326 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
327 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
328 |
> |
final CountDownLatch done = new CountDownLatch(1); |
329 |
> |
try { |
330 |
> |
assertEquals(0, p.getActiveCount()); |
331 |
> |
p.execute(new CheckedRunnable() { |
332 |
> |
public void realRun() throws InterruptedException { |
333 |
> |
threadStarted.countDown(); |
334 |
> |
assertEquals(1, p.getActiveCount()); |
335 |
> |
done.await(); |
336 |
> |
}}); |
337 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
338 |
> |
assertEquals(1, p.getActiveCount()); |
339 |
> |
} finally { |
340 |
> |
done.countDown(); |
341 |
> |
joinPool(p); |
342 |
> |
} |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
347 |
< |
* when tasks complete |
346 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
347 |
> |
* when tasks complete |
348 |
|
*/ |
349 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
350 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
351 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
352 |
< |
p2.execute(new SmallRunnable()); |
353 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
354 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
355 |
< |
joinPool(p2); |
350 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
351 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
352 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
353 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
354 |
> |
try { |
355 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
356 |
> |
p.execute(new CheckedRunnable() { |
357 |
> |
public void realRun() throws InterruptedException { |
358 |
> |
threadStarted.countDown(); |
359 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
360 |
> |
threadProceed.await(); |
361 |
> |
threadDone.countDown(); |
362 |
> |
}}); |
363 |
> |
await(threadStarted); |
364 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
365 |
> |
threadProceed.countDown(); |
366 |
> |
threadDone.await(); |
367 |
> |
long startTime = System.nanoTime(); |
368 |
> |
while (p.getCompletedTaskCount() != 1) { |
369 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
370 |
> |
fail("timed out"); |
371 |
> |
Thread.yield(); |
372 |
> |
} |
373 |
> |
} finally { |
374 |
> |
joinPool(p); |
375 |
> |
} |
376 |
|
} |
377 |
|
|
378 |
|
/** |
379 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
379 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
380 |
|
*/ |
381 |
|
public void testGetCorePoolSize() throws InterruptedException { |
382 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
383 |
< |
assertEquals(1, p1.getCorePoolSize()); |
384 |
< |
joinPool(p1); |
382 |
> |
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
383 |
> |
assertEquals(1, p.getCorePoolSize()); |
384 |
> |
joinPool(p); |
385 |
|
} |
386 |
|
|
387 |
|
/** |
388 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
389 |
< |
* multiple threads active |
388 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
389 |
> |
* multiple threads active |
390 |
|
*/ |
391 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
392 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
393 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
394 |
< |
p2.execute(new SmallRunnable()); |
395 |
< |
p2.execute(new SmallRunnable()); |
396 |
< |
Thread.sleep(SHORT_DELAY_MS); |
397 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
398 |
< |
joinPool(p2); |
392 |
> |
final int THREADS = 3; |
393 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
394 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
395 |
> |
final CountDownLatch done = new CountDownLatch(1); |
396 |
> |
try { |
397 |
> |
assertEquals(0, p.getLargestPoolSize()); |
398 |
> |
for (int i = 0; i < THREADS; i++) |
399 |
> |
p.execute(new CheckedRunnable() { |
400 |
> |
public void realRun() throws InterruptedException { |
401 |
> |
threadsStarted.countDown(); |
402 |
> |
done.await(); |
403 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
404 |
> |
}}); |
405 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
406 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
407 |
> |
} finally { |
408 |
> |
done.countDown(); |
409 |
> |
joinPool(p); |
410 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
411 |
> |
} |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
416 |
< |
* become active |
415 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
416 |
> |
* become active |
417 |
|
*/ |
418 |
|
public void testGetPoolSize() throws InterruptedException { |
419 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
420 |
< |
assertEquals(0, p1.getPoolSize()); |
421 |
< |
p1.execute(new SmallRunnable()); |
422 |
< |
assertEquals(1, p1.getPoolSize()); |
423 |
< |
joinPool(p1); |
419 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
420 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
421 |
> |
final CountDownLatch done = new CountDownLatch(1); |
422 |
> |
try { |
423 |
> |
assertEquals(0, p.getPoolSize()); |
424 |
> |
p.execute(new CheckedRunnable() { |
425 |
> |
public void realRun() throws InterruptedException { |
426 |
> |
threadStarted.countDown(); |
427 |
> |
assertEquals(1, p.getPoolSize()); |
428 |
> |
done.await(); |
429 |
> |
}}); |
430 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
431 |
> |
assertEquals(1, p.getPoolSize()); |
432 |
> |
} finally { |
433 |
> |
done.countDown(); |
434 |
> |
joinPool(p); |
435 |
> |
} |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
440 |
< |
* submitted |
439 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
440 |
> |
* submitted |
441 |
|
*/ |
442 |
|
public void testGetTaskCount() throws InterruptedException { |
443 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
444 |
< |
assertEquals(0, p1.getTaskCount()); |
445 |
< |
for (int i = 0; i < 5; i++) |
446 |
< |
p1.execute(new SmallRunnable()); |
447 |
< |
Thread.sleep(SHORT_DELAY_MS); |
448 |
< |
assertEquals(5, p1.getTaskCount()); |
449 |
< |
joinPool(p1); |
443 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
444 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
445 |
> |
final CountDownLatch done = new CountDownLatch(1); |
446 |
> |
final int TASKS = 5; |
447 |
> |
try { |
448 |
> |
assertEquals(0, p.getTaskCount()); |
449 |
> |
for (int i = 0; i < TASKS; i++) |
450 |
> |
p.execute(new CheckedRunnable() { |
451 |
> |
public void realRun() throws InterruptedException { |
452 |
> |
threadStarted.countDown(); |
453 |
> |
done.await(); |
454 |
> |
}}); |
455 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
456 |
> |
assertEquals(TASKS, p.getTaskCount()); |
457 |
> |
} finally { |
458 |
> |
done.countDown(); |
459 |
> |
joinPool(p); |
460 |
> |
} |
461 |
|
} |
462 |
|
|
463 |
|
/** |
465 |
|
*/ |
466 |
|
public void testGetThreadFactory() throws InterruptedException { |
467 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
468 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
468 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
469 |
|
assertSame(tf, p.getThreadFactory()); |
470 |
|
joinPool(p); |
471 |
|
} |
475 |
|
*/ |
476 |
|
public void testSetThreadFactory() throws InterruptedException { |
477 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
478 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
478 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
479 |
|
p.setThreadFactory(tf); |
480 |
|
assertSame(tf, p.getThreadFactory()); |
481 |
|
joinPool(p); |
485 |
|
* setThreadFactory(null) throws NPE |
486 |
|
*/ |
487 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
488 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
488 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
489 |
|
try { |
490 |
|
p.setThreadFactory(null); |
491 |
|
shouldThrow(); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* is isShutDown is false before shutdown, true after |
499 |
> |
* isShutdown is false before shutdown, true after |
500 |
|
*/ |
501 |
|
public void testIsShutdown() { |
502 |
|
|
503 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
503 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
504 |
|
try { |
505 |
< |
assertFalse(p1.isShutdown()); |
505 |
> |
assertFalse(p.isShutdown()); |
506 |
|
} |
507 |
|
finally { |
508 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
508 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
509 |
|
} |
510 |
< |
assertTrue(p1.isShutdown()); |
510 |
> |
assertTrue(p.isShutdown()); |
511 |
|
} |
512 |
|
|
373 |
– |
|
513 |
|
/** |
514 |
< |
* isTerminated is false before termination, true after |
514 |
> |
* isTerminated is false before termination, true after |
515 |
|
*/ |
516 |
|
public void testIsTerminated() throws InterruptedException { |
517 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
517 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
518 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
519 |
> |
final CountDownLatch done = new CountDownLatch(1); |
520 |
> |
assertFalse(p.isTerminated()); |
521 |
|
try { |
522 |
< |
p1.execute(new SmallRunnable()); |
522 |
> |
p.execute(new CheckedRunnable() { |
523 |
> |
public void realRun() throws InterruptedException { |
524 |
> |
assertFalse(p.isTerminated()); |
525 |
> |
threadStarted.countDown(); |
526 |
> |
done.await(); |
527 |
> |
}}); |
528 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
529 |
> |
assertFalse(p.isTerminating()); |
530 |
> |
done.countDown(); |
531 |
|
} finally { |
532 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
532 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
533 |
|
} |
534 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
535 |
< |
assertTrue(p1.isTerminated()); |
534 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
535 |
> |
assertTrue(p.isTerminated()); |
536 |
|
} |
537 |
|
|
538 |
|
/** |
539 |
< |
* isTerminating is not true when running or when terminated |
539 |
> |
* isTerminating is not true when running or when terminated |
540 |
|
*/ |
541 |
|
public void testIsTerminating() throws InterruptedException { |
542 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
543 |
< |
assertFalse(p1.isTerminating()); |
544 |
< |
try { |
545 |
< |
p1.execute(new SmallRunnable()); |
546 |
< |
assertFalse(p1.isTerminating()); |
547 |
< |
} finally { |
548 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
549 |
< |
} |
550 |
< |
|
551 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
552 |
< |
assertTrue(p1.isTerminated()); |
553 |
< |
assertFalse(p1.isTerminating()); |
542 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
543 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
544 |
> |
final CountDownLatch done = new CountDownLatch(1); |
545 |
> |
try { |
546 |
> |
assertFalse(p.isTerminating()); |
547 |
> |
p.execute(new CheckedRunnable() { |
548 |
> |
public void realRun() throws InterruptedException { |
549 |
> |
assertFalse(p.isTerminating()); |
550 |
> |
threadStarted.countDown(); |
551 |
> |
done.await(); |
552 |
> |
}}); |
553 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
554 |
> |
assertFalse(p.isTerminating()); |
555 |
> |
done.countDown(); |
556 |
> |
} finally { |
557 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
558 |
> |
} |
559 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
560 |
> |
assertTrue(p.isTerminated()); |
561 |
> |
assertFalse(p.isTerminating()); |
562 |
|
} |
563 |
|
|
564 |
|
/** |
565 |
|
* getQueue returns the work queue, which contains queued tasks |
566 |
|
*/ |
567 |
|
public void testGetQueue() throws InterruptedException { |
568 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
569 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
570 |
< |
for (int i = 0; i < 5; i++) { |
571 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
572 |
< |
} |
573 |
< |
try { |
574 |
< |
Thread.sleep(SHORT_DELAY_MS); |
575 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
576 |
< |
assertTrue(q.contains(tasks[4])); |
568 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
569 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
570 |
> |
final CountDownLatch done = new CountDownLatch(1); |
571 |
> |
try { |
572 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
573 |
> |
for (int i = 0; i < tasks.length; i++) { |
574 |
> |
Runnable r = new CheckedRunnable() { |
575 |
> |
public void realRun() throws InterruptedException { |
576 |
> |
threadStarted.countDown(); |
577 |
> |
done.await(); |
578 |
> |
}}; |
579 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
580 |
> |
} |
581 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
582 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
583 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
584 |
|
assertFalse(q.contains(tasks[0])); |
585 |
|
} finally { |
586 |
< |
joinPool(p1); |
586 |
> |
done.countDown(); |
587 |
> |
joinPool(p); |
588 |
|
} |
589 |
|
} |
590 |
|
|
592 |
|
* remove(task) removes queued task, and fails to remove active task |
593 |
|
*/ |
594 |
|
public void testRemove() throws InterruptedException { |
595 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
595 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
596 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
597 |
< |
for (int i = 0; i < 5; i++) { |
598 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
433 |
< |
} |
597 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
598 |
> |
final CountDownLatch done = new CountDownLatch(1); |
599 |
|
try { |
600 |
< |
Thread.sleep(SHORT_DELAY_MS); |
601 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
602 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
600 |
> |
for (int i = 0; i < tasks.length; i++) { |
601 |
> |
Runnable r = new CheckedRunnable() { |
602 |
> |
public void realRun() throws InterruptedException { |
603 |
> |
threadStarted.countDown(); |
604 |
> |
done.await(); |
605 |
> |
}}; |
606 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
607 |
> |
} |
608 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
609 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
610 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
611 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
612 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
613 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
614 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
613 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
614 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
615 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
616 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
617 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
617 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
618 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
619 |
|
} finally { |
620 |
< |
joinPool(p1); |
620 |
> |
done.countDown(); |
621 |
> |
joinPool(p); |
622 |
|
} |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
< |
* purge removes cancelled tasks from the queue |
626 |
> |
* purge eventually removes cancelled tasks from the queue |
627 |
|
*/ |
628 |
|
public void testPurge() throws InterruptedException { |
629 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
629 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
630 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
631 |
< |
for (int i = 0; i < 5; i++) { |
632 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
633 |
< |
} |
631 |
> |
for (int i = 0; i < tasks.length; i++) |
632 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
633 |
> |
LONG_DELAY_MS, MILLISECONDS); |
634 |
|
try { |
635 |
< |
int max = 5; |
635 |
> |
int max = tasks.length; |
636 |
|
if (tasks[4].cancel(true)) --max; |
637 |
|
if (tasks[3].cancel(true)) --max; |
638 |
|
// There must eventually be an interference-free point at |
639 |
|
// which purge will not fail. (At worst, when queue is empty.) |
640 |
< |
int k; |
641 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
642 |
< |
p1.purge(); |
643 |
< |
long count = p1.getTaskCount(); |
644 |
< |
if (count >= 0 && count <= max) |
645 |
< |
break; |
646 |
< |
Thread.sleep(1); |
647 |
< |
} |
474 |
< |
assertTrue(k < SMALL_DELAY_MS); |
640 |
> |
long startTime = System.nanoTime(); |
641 |
> |
do { |
642 |
> |
p.purge(); |
643 |
> |
long count = p.getTaskCount(); |
644 |
> |
if (count == max) |
645 |
> |
return; |
646 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
647 |
> |
fail("Purge failed to remove cancelled tasks"); |
648 |
|
} finally { |
649 |
< |
joinPool(p1); |
649 |
> |
for (ScheduledFuture task : tasks) |
650 |
> |
task.cancel(true); |
651 |
> |
joinPool(p); |
652 |
|
} |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
< |
* shutDownNow returns a list containing tasks that were not run |
656 |
> |
* shutdownNow returns a list containing tasks that were not run, |
657 |
> |
* and those tasks are drained from the queue |
658 |
|
*/ |
659 |
< |
public void testShutDownNow() throws InterruptedException { |
660 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
661 |
< |
for (int i = 0; i < 5; i++) |
662 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
663 |
< |
List l; |
659 |
> |
public void testShutdownNow() throws InterruptedException { |
660 |
> |
final int poolSize = 2; |
661 |
> |
final int count = 5; |
662 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
663 |
> |
final ScheduledThreadPoolExecutor p = |
664 |
> |
new ScheduledThreadPoolExecutor(poolSize); |
665 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
666 |
> |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
667 |
> |
threadsStarted.countDown(); |
668 |
> |
try { |
669 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
670 |
> |
} catch (InterruptedException success) {} |
671 |
> |
ran.getAndIncrement(); |
672 |
> |
}}; |
673 |
> |
for (int i = 0; i < count; i++) |
674 |
> |
p.execute(waiter); |
675 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
676 |
> |
assertEquals(poolSize, p.getActiveCount()); |
677 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
678 |
> |
final List<Runnable> queuedTasks; |
679 |
> |
try { |
680 |
> |
queuedTasks = p.shutdownNow(); |
681 |
> |
} catch (SecurityException ok) { |
682 |
> |
return; // Allowed in case test doesn't have privs |
683 |
> |
} |
684 |
> |
assertTrue(p.isShutdown()); |
685 |
> |
assertTrue(p.getQueue().isEmpty()); |
686 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
687 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
688 |
> |
assertTrue(p.isTerminated()); |
689 |
> |
assertEquals(poolSize, ran.get()); |
690 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
691 |
> |
} |
692 |
> |
|
693 |
> |
/** |
694 |
> |
* shutdownNow returns a list containing tasks that were not run, |
695 |
> |
* and those tasks are drained from the queue |
696 |
> |
*/ |
697 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
698 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
699 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
700 |
> |
for (int i = 0; i < 3; i++) { |
701 |
> |
Runnable r = new NoOpRunnable(); |
702 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
703 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
704 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
705 |
> |
} |
706 |
> |
if (testImplementationDetails) |
707 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
708 |
> |
final List<Runnable> queuedTasks; |
709 |
|
try { |
710 |
< |
l = p1.shutdownNow(); |
710 |
> |
queuedTasks = p.shutdownNow(); |
711 |
|
} catch (SecurityException ok) { |
712 |
< |
return; |
712 |
> |
return; // Allowed in case test doesn't have privs |
713 |
> |
} |
714 |
> |
assertTrue(p.isShutdown()); |
715 |
> |
assertTrue(p.getQueue().isEmpty()); |
716 |
> |
if (testImplementationDetails) |
717 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
718 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
719 |
> |
for (ScheduledFuture task : tasks) { |
720 |
> |
assertFalse(task.isDone()); |
721 |
> |
assertFalse(task.isCancelled()); |
722 |
|
} |
723 |
< |
assertTrue(p1.isShutdown()); |
724 |
< |
assertTrue(l.size() > 0 && l.size() <= 5); |
495 |
< |
joinPool(p1); |
723 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
724 |
> |
assertTrue(p.isTerminated()); |
725 |
|
} |
726 |
|
|
727 |
|
/** |
728 |
|
* In default setting, shutdown cancels periodic but not delayed |
729 |
|
* tasks at shutdown |
730 |
|
*/ |
731 |
< |
public void testShutDown1() throws InterruptedException { |
732 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
733 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
734 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
731 |
> |
public void testShutdown1() throws InterruptedException { |
732 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
733 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
734 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
735 |
|
|
736 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
737 |
< |
for (int i = 0; i < 5; i++) |
738 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
739 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
740 |
< |
BlockingQueue q = p1.getQueue(); |
741 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
742 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
743 |
< |
assertFalse(t.isCancelled()); |
744 |
< |
} |
745 |
< |
assertTrue(p1.isShutdown()); |
746 |
< |
Thread.sleep(SMALL_DELAY_MS); |
747 |
< |
for (int i = 0; i < 5; ++i) { |
748 |
< |
assertTrue(tasks[i].isDone()); |
749 |
< |
assertFalse(tasks[i].isCancelled()); |
737 |
> |
for (int i = 0; i < tasks.length; i++) |
738 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
739 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
740 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
741 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
742 |
> |
for (ScheduledFuture task : tasks) { |
743 |
> |
assertFalse(task.isDone()); |
744 |
> |
assertFalse(task.isCancelled()); |
745 |
> |
assertTrue(q.contains(task)); |
746 |
> |
} |
747 |
> |
assertTrue(p.isShutdown()); |
748 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
749 |
> |
assertTrue(p.isTerminated()); |
750 |
> |
for (ScheduledFuture task : tasks) { |
751 |
> |
assertTrue(task.isDone()); |
752 |
> |
assertFalse(task.isCancelled()); |
753 |
|
} |
754 |
|
} |
755 |
|
|
524 |
– |
|
756 |
|
/** |
757 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
758 |
|
* delayed tasks are cancelled at shutdown |
759 |
|
*/ |
760 |
< |
public void testShutDown2() throws InterruptedException { |
761 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
762 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
760 |
> |
public void testShutdown2() throws InterruptedException { |
761 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
762 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
763 |
> |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
764 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
765 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
766 |
< |
for (int i = 0; i < 5; i++) |
767 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
768 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
769 |
< |
assertTrue(p1.isShutdown()); |
770 |
< |
BlockingQueue q = p1.getQueue(); |
766 |
> |
for (int i = 0; i < tasks.length; i++) |
767 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
768 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
769 |
> |
BlockingQueue q = p.getQueue(); |
770 |
> |
assertEquals(tasks.length, q.size()); |
771 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
772 |
> |
assertTrue(p.isShutdown()); |
773 |
|
assertTrue(q.isEmpty()); |
774 |
< |
Thread.sleep(SMALL_DELAY_MS); |
775 |
< |
assertTrue(p1.isTerminated()); |
774 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
775 |
> |
assertTrue(p.isTerminated()); |
776 |
> |
for (ScheduledFuture task : tasks) { |
777 |
> |
assertTrue(task.isDone()); |
778 |
> |
assertTrue(task.isCancelled()); |
779 |
> |
} |
780 |
|
} |
781 |
|
|
543 |
– |
|
782 |
|
/** |
783 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
784 |
< |
* periodic tasks are not cancelled at shutdown |
784 |
> |
* periodic tasks are cancelled at shutdown |
785 |
|
*/ |
786 |
< |
public void testShutDown3() throws InterruptedException { |
787 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
788 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
786 |
> |
public void testShutdown3() throws InterruptedException { |
787 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
788 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
789 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
790 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
791 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
792 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
793 |
> |
long initialDelay = LONG_DELAY_MS; |
794 |
|
ScheduledFuture task = |
795 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
796 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
797 |
< |
assertTrue(p1.isShutdown()); |
798 |
< |
BlockingQueue q = p1.getQueue(); |
799 |
< |
assertTrue(q.isEmpty()); |
800 |
< |
Thread.sleep(SHORT_DELAY_MS); |
801 |
< |
assertTrue(p1.isTerminated()); |
795 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
796 |
> |
5, MILLISECONDS); |
797 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
798 |
> |
assertTrue(p.isShutdown()); |
799 |
> |
assertTrue(p.getQueue().isEmpty()); |
800 |
> |
assertTrue(task.isDone()); |
801 |
> |
assertTrue(task.isCancelled()); |
802 |
> |
joinPool(p); |
803 |
|
} |
804 |
|
|
805 |
|
/** |
806 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
807 |
< |
* periodic tasks are cancelled at shutdown |
807 |
> |
* periodic tasks are not cancelled at shutdown |
808 |
|
*/ |
809 |
< |
public void testShutDown4() throws InterruptedException { |
810 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
811 |
< |
try { |
812 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
809 |
> |
public void testShutdown4() throws InterruptedException { |
810 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
811 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
812 |
> |
try { |
813 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
814 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
815 |
> |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
816 |
> |
final Runnable r = new CheckedRunnable() { |
817 |
> |
public void realRun() { |
818 |
> |
counter.countDown(); |
819 |
> |
}}; |
820 |
|
ScheduledFuture task = |
821 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
821 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
822 |
> |
assertFalse(task.isDone()); |
823 |
|
assertFalse(task.isCancelled()); |
824 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
824 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
825 |
|
assertFalse(task.isCancelled()); |
826 |
< |
assertFalse(p1.isTerminated()); |
827 |
< |
assertTrue(p1.isShutdown()); |
828 |
< |
Thread.sleep(SHORT_DELAY_MS); |
826 |
> |
assertFalse(p.isTerminated()); |
827 |
> |
assertTrue(p.isShutdown()); |
828 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
829 |
|
assertFalse(task.isCancelled()); |
830 |
< |
assertTrue(task.cancel(true)); |
830 |
> |
assertTrue(task.cancel(false)); |
831 |
|
assertTrue(task.isDone()); |
832 |
< |
Thread.sleep(SHORT_DELAY_MS); |
833 |
< |
assertTrue(p1.isTerminated()); |
832 |
> |
assertTrue(task.isCancelled()); |
833 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
834 |
> |
assertTrue(p.isTerminated()); |
835 |
|
} |
836 |
|
finally { |
837 |
< |
joinPool(p1); |
837 |
> |
joinPool(p); |
838 |
|
} |
839 |
|
} |
840 |
|
|
912 |
|
* invokeAny(c) throws NPE if c has null elements |
913 |
|
*/ |
914 |
|
public void testInvokeAny3() throws Exception { |
915 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
915 |
> |
CountDownLatch latch = new CountDownLatch(1); |
916 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
917 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
918 |
+ |
l.add(latchAwaitingStringTask(latch)); |
919 |
+ |
l.add(null); |
920 |
|
try { |
665 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
666 |
– |
l.add(new Callable<String>() { |
667 |
– |
public String call() { |
668 |
– |
try { |
669 |
– |
latch.await(); |
670 |
– |
} catch (InterruptedException ok) {} |
671 |
– |
return TEST_STRING; |
672 |
– |
}}); |
673 |
– |
l.add(null); |
921 |
|
e.invokeAny(l); |
922 |
|
shouldThrow(); |
923 |
|
} catch (NullPointerException success) { |
932 |
|
*/ |
933 |
|
public void testInvokeAny4() throws Exception { |
934 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
935 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
936 |
+ |
l.add(new NPETask()); |
937 |
|
try { |
689 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
690 |
– |
l.add(new NPETask()); |
938 |
|
e.invokeAny(l); |
939 |
|
shouldThrow(); |
940 |
|
} catch (ExecutionException success) { |
950 |
|
public void testInvokeAny5() throws Exception { |
951 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
952 |
|
try { |
953 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
953 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
954 |
|
l.add(new StringTask()); |
955 |
|
l.add(new StringTask()); |
956 |
|
String result = e.invokeAny(l); |
992 |
|
*/ |
993 |
|
public void testInvokeAll3() throws Exception { |
994 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
995 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
996 |
+ |
l.add(new StringTask()); |
997 |
+ |
l.add(null); |
998 |
|
try { |
749 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
750 |
– |
l.add(new StringTask()); |
751 |
– |
l.add(null); |
999 |
|
e.invokeAll(l); |
1000 |
|
shouldThrow(); |
1001 |
|
} catch (NullPointerException success) { |
1009 |
|
*/ |
1010 |
|
public void testInvokeAll4() throws Exception { |
1011 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1012 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1013 |
+ |
l.add(new NPETask()); |
1014 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
1015 |
+ |
assertEquals(1, futures.size()); |
1016 |
|
try { |
1017 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
767 |
< |
l.add(new NPETask()); |
768 |
< |
List<Future<String>> result = e.invokeAll(l); |
769 |
< |
assertEquals(1, result.size()); |
770 |
< |
for (Future<String> future : result) |
771 |
< |
future.get(); |
1017 |
> |
futures.get(0).get(); |
1018 |
|
shouldThrow(); |
1019 |
|
} catch (ExecutionException success) { |
1020 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1029 |
|
public void testInvokeAll5() throws Exception { |
1030 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1031 |
|
try { |
1032 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1032 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1033 |
|
l.add(new StringTask()); |
1034 |
|
l.add(new StringTask()); |
1035 |
< |
List<Future<String>> result = e.invokeAll(l); |
1036 |
< |
assertEquals(2, result.size()); |
1037 |
< |
for (Future<String> future : result) |
1035 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1036 |
> |
assertEquals(2, futures.size()); |
1037 |
> |
for (Future<String> future : futures) |
1038 |
|
assertSame(TEST_STRING, future.get()); |
1039 |
|
} finally { |
1040 |
|
joinPool(e); |
1060 |
|
*/ |
1061 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1062 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1063 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1064 |
+ |
l.add(new StringTask()); |
1065 |
|
try { |
818 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
819 |
– |
l.add(new StringTask()); |
1066 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1067 |
|
shouldThrow(); |
1068 |
|
} catch (NullPointerException success) { |
1089 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1090 |
|
*/ |
1091 |
|
public void testTimedInvokeAny3() throws Exception { |
1092 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
1092 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1093 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1094 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1095 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1096 |
+ |
l.add(null); |
1097 |
|
try { |
849 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
850 |
– |
l.add(new Callable<String>() { |
851 |
– |
public String call() { |
852 |
– |
try { |
853 |
– |
latch.await(); |
854 |
– |
} catch (InterruptedException ok) {} |
855 |
– |
return TEST_STRING; |
856 |
– |
}}); |
857 |
– |
l.add(null); |
1098 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1099 |
|
shouldThrow(); |
1100 |
|
} catch (NullPointerException success) { |
1109 |
|
*/ |
1110 |
|
public void testTimedInvokeAny4() throws Exception { |
1111 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1112 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1113 |
+ |
l.add(new NPETask()); |
1114 |
|
try { |
873 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
874 |
– |
l.add(new NPETask()); |
1115 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1116 |
|
shouldThrow(); |
1117 |
|
} catch (ExecutionException success) { |
1127 |
|
public void testTimedInvokeAny5() throws Exception { |
1128 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1129 |
|
try { |
1130 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1130 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1131 |
|
l.add(new StringTask()); |
1132 |
|
l.add(new StringTask()); |
1133 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1156 |
|
*/ |
1157 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1158 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1159 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1160 |
+ |
l.add(new StringTask()); |
1161 |
|
try { |
920 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
921 |
– |
l.add(new StringTask()); |
1162 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1163 |
|
shouldThrow(); |
1164 |
|
} catch (NullPointerException success) { |
1185 |
|
*/ |
1186 |
|
public void testTimedInvokeAll3() throws Exception { |
1187 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1188 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1189 |
+ |
l.add(new StringTask()); |
1190 |
+ |
l.add(null); |
1191 |
|
try { |
949 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
950 |
– |
l.add(new StringTask()); |
951 |
– |
l.add(null); |
1192 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1193 |
|
shouldThrow(); |
1194 |
|
} catch (NullPointerException success) { |
1202 |
|
*/ |
1203 |
|
public void testTimedInvokeAll4() throws Exception { |
1204 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1205 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1206 |
+ |
l.add(new NPETask()); |
1207 |
+ |
List<Future<String>> futures = |
1208 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1209 |
+ |
assertEquals(1, futures.size()); |
1210 |
|
try { |
1211 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
967 |
< |
l.add(new NPETask()); |
968 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
969 |
< |
assertEquals(1, result.size()); |
970 |
< |
for (Future<String> future : result) |
971 |
< |
future.get(); |
1211 |
> |
futures.get(0).get(); |
1212 |
|
shouldThrow(); |
1213 |
|
} catch (ExecutionException success) { |
1214 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1223 |
|
public void testTimedInvokeAll5() throws Exception { |
1224 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1225 |
|
try { |
1226 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1226 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1227 |
|
l.add(new StringTask()); |
1228 |
|
l.add(new StringTask()); |
1229 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1230 |
< |
assertEquals(2, result.size()); |
1231 |
< |
for (Future<String> future : result) |
1229 |
> |
List<Future<String>> futures = |
1230 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1231 |
> |
assertEquals(2, futures.size()); |
1232 |
> |
for (Future<String> future : futures) |
1233 |
|
assertSame(TEST_STRING, future.get()); |
1234 |
|
} finally { |
1235 |
|
joinPool(e); |
1242 |
|
public void testTimedInvokeAll6() throws Exception { |
1243 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1244 |
|
try { |
1245 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1246 |
< |
l.add(new StringTask()); |
1247 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1248 |
< |
l.add(new StringTask()); |
1249 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1250 |
< |
assertEquals(3, result.size()); |
1251 |
< |
Iterator<Future<String>> it = result.iterator(); |
1252 |
< |
Future<String> f1 = it.next(); |
1253 |
< |
Future<String> f2 = it.next(); |
1254 |
< |
Future<String> f3 = it.next(); |
1255 |
< |
assertTrue(f1.isDone()); |
1256 |
< |
assertTrue(f2.isDone()); |
1257 |
< |
assertTrue(f3.isDone()); |
1258 |
< |
assertFalse(f1.isCancelled()); |
1259 |
< |
assertTrue(f2.isCancelled()); |
1245 |
> |
for (long timeout = timeoutMillis();;) { |
1246 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1247 |
> |
tasks.add(new StringTask("0")); |
1248 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1249 |
> |
tasks.add(new StringTask("2")); |
1250 |
> |
long startTime = System.nanoTime(); |
1251 |
> |
List<Future<String>> futures = |
1252 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1253 |
> |
assertEquals(tasks.size(), futures.size()); |
1254 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1255 |
> |
for (Future future : futures) |
1256 |
> |
assertTrue(future.isDone()); |
1257 |
> |
assertTrue(futures.get(1).isCancelled()); |
1258 |
> |
try { |
1259 |
> |
assertEquals("0", futures.get(0).get()); |
1260 |
> |
assertEquals("2", futures.get(2).get()); |
1261 |
> |
break; |
1262 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1263 |
> |
timeout *= 2; |
1264 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1265 |
> |
fail("expected exactly one task to be cancelled"); |
1266 |
> |
} |
1267 |
> |
} |
1268 |
|
} finally { |
1269 |
|
joinPool(e); |
1270 |
|
} |
1271 |
|
} |
1272 |
|
|
1024 |
– |
|
1273 |
|
} |