1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
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.*; |
12 |
+ |
import java.util.concurrent.atomic.*; |
13 |
|
|
14 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
30 |
|
p1.execute(runnable); |
31 |
|
assertFalse(runnable.done); |
32 |
|
Thread.sleep(SHORT_DELAY_MS); |
33 |
< |
p1.shutdown(); |
33 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
34 |
|
try { |
35 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
36 |
|
} catch(InterruptedException e){ |
37 |
|
unexpectedException(); |
38 |
|
} |
39 |
|
assertTrue(runnable.done); |
40 |
< |
p1.shutdown(); |
40 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
41 |
|
joinPool(p1); |
42 |
|
} |
43 |
|
catch(Exception e){ |
59 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
60 |
|
assertTrue(callable.done); |
61 |
|
assertEquals(Boolean.TRUE, f.get()); |
62 |
< |
p1.shutdown(); |
62 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
63 |
|
joinPool(p1); |
64 |
|
} catch(RejectedExecutionException e){} |
65 |
|
catch(Exception e){ |
80 |
|
assertFalse(runnable.done); |
81 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
82 |
|
assertTrue(runnable.done); |
83 |
< |
p1.shutdown(); |
83 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
84 |
|
joinPool(p1); |
85 |
|
} catch(Exception e){ |
86 |
|
unexpectedException(); |
99 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
100 |
|
assertTrue(runnable.done); |
101 |
|
h.cancel(true); |
100 |
– |
p1.shutdown(); |
102 |
|
joinPool(p1); |
103 |
|
} catch(Exception e){ |
104 |
|
unexpectedException(); |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
+ |
static class RunnableCounter implements Runnable { |
109 |
+ |
AtomicInteger count = new AtomicInteger(0); |
110 |
+ |
public void run() { count.getAndIncrement(); } |
111 |
+ |
} |
112 |
+ |
|
113 |
|
/** |
114 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
115 |
|
*/ |
122 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
123 |
|
assertTrue(runnable.done); |
124 |
|
h.cancel(true); |
119 |
– |
p1.shutdown(); |
125 |
|
joinPool(p1); |
126 |
|
} catch(Exception e){ |
127 |
|
unexpectedException(); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
+ |
* scheduleAtFixedRate executes series of tasks at given rate |
133 |
+ |
*/ |
134 |
+ |
public void testFixedRateSequence() { |
135 |
+ |
try { |
136 |
+ |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
137 |
+ |
RunnableCounter counter = new RunnableCounter(); |
138 |
+ |
ScheduledFuture h = |
139 |
+ |
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); |
140 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
141 |
+ |
h.cancel(true); |
142 |
+ |
int c = counter.count.get(); |
143 |
+ |
// By time scaling conventions, we must have at least |
144 |
+ |
// an execution per SHORT delay, but no more than one SHORT more |
145 |
+ |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
146 |
+ |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
147 |
+ |
joinPool(p1); |
148 |
+ |
} catch(Exception e){ |
149 |
+ |
unexpectedException(); |
150 |
+ |
} |
151 |
+ |
} |
152 |
+ |
|
153 |
+ |
/** |
154 |
+ |
* scheduleWithFixedDelay executes series of tasks with given period |
155 |
+ |
*/ |
156 |
+ |
public void testFixedDelaySequence() { |
157 |
+ |
try { |
158 |
+ |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
159 |
+ |
RunnableCounter counter = new RunnableCounter(); |
160 |
+ |
ScheduledFuture h = |
161 |
+ |
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
162 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
163 |
+ |
h.cancel(true); |
164 |
+ |
int c = counter.count.get(); |
165 |
+ |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
166 |
+ |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
167 |
+ |
joinPool(p1); |
168 |
+ |
} catch(Exception e){ |
169 |
+ |
unexpectedException(); |
170 |
+ |
} |
171 |
+ |
} |
172 |
+ |
|
173 |
+ |
|
174 |
+ |
/** |
175 |
|
* execute (null) throws NPE |
176 |
|
*/ |
177 |
|
public void testExecuteNull() { |
215 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
216 |
|
shouldThrow(); |
217 |
|
} catch(RejectedExecutionException success){ |
218 |
+ |
} catch (SecurityException ok) { |
219 |
|
} |
220 |
+ |
|
221 |
|
joinPool(se); |
222 |
|
|
223 |
|
} |
233 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
234 |
|
shouldThrow(); |
235 |
|
} catch(RejectedExecutionException success){ |
236 |
+ |
} catch (SecurityException ok) { |
237 |
|
} |
238 |
|
joinPool(se); |
239 |
|
} |
249 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
250 |
|
shouldThrow(); |
251 |
|
} catch(RejectedExecutionException success){ |
252 |
< |
} |
252 |
> |
} catch (SecurityException ok) { |
253 |
> |
} |
254 |
|
joinPool(se); |
255 |
|
} |
256 |
|
|
265 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
266 |
|
shouldThrow(); |
267 |
|
} catch(RejectedExecutionException success){ |
268 |
+ |
} catch (SecurityException ok) { |
269 |
|
} |
270 |
|
joinPool(se); |
271 |
|
} |
281 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
282 |
|
shouldThrow(); |
283 |
|
} catch(RejectedExecutionException success){ |
284 |
+ |
} catch (SecurityException ok) { |
285 |
|
} |
286 |
|
joinPool(se); |
287 |
|
} |
384 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
385 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
386 |
|
assertSame(tf, p.getThreadFactory()); |
333 |
– |
p.shutdown(); |
387 |
|
joinPool(p); |
388 |
|
} |
389 |
|
|
395 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
396 |
|
p.setThreadFactory(tf); |
397 |
|
assertSame(tf, p.getThreadFactory()); |
345 |
– |
p.shutdown(); |
398 |
|
joinPool(p); |
399 |
|
} |
400 |
|
|
422 |
|
assertFalse(p1.isShutdown()); |
423 |
|
} |
424 |
|
finally { |
425 |
< |
p1.shutdown(); |
425 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
426 |
|
} |
427 |
|
assertTrue(p1.isShutdown()); |
428 |
|
} |
436 |
|
try { |
437 |
|
p1.execute(new SmallRunnable()); |
438 |
|
} finally { |
439 |
< |
p1.shutdown(); |
439 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
440 |
|
} |
441 |
|
try { |
442 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
456 |
|
p1.execute(new SmallRunnable()); |
457 |
|
assertFalse(p1.isTerminating()); |
458 |
|
} finally { |
459 |
< |
p1.shutdown(); |
459 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
460 |
|
} |
461 |
|
try { |
462 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
481 |
|
BlockingQueue<Runnable> q = p1.getQueue(); |
482 |
|
assertTrue(q.contains(tasks[4])); |
483 |
|
assertFalse(q.contains(tasks[0])); |
432 |
– |
p1.shutdownNow(); |
484 |
|
} catch(Exception e) { |
485 |
|
unexpectedException(); |
486 |
|
} finally { |
509 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
510 |
|
assertTrue(p1.remove((Runnable)tasks[3])); |
511 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
461 |
– |
p1.shutdownNow(); |
512 |
|
} catch(Exception e) { |
513 |
|
unexpectedException(); |
514 |
|
} finally { |
523 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
524 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
525 |
|
for(int i = 0; i < 5; i++){ |
526 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS); |
526 |
> |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
527 |
> |
} |
528 |
> |
try { |
529 |
> |
int max = 5; |
530 |
> |
if (tasks[4].cancel(true)) --max; |
531 |
> |
if (tasks[3].cancel(true)) --max; |
532 |
> |
// There must eventually be an interference-free point at |
533 |
> |
// which purge will not fail. (At worst, when queue is empty.) |
534 |
> |
int k; |
535 |
> |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
536 |
> |
p1.purge(); |
537 |
> |
long count = p1.getTaskCount(); |
538 |
> |
if (count >= 0 && count <= max) |
539 |
> |
break; |
540 |
> |
Thread.sleep(1); |
541 |
> |
} |
542 |
> |
assertTrue(k < SMALL_DELAY_MS); |
543 |
> |
} catch(Exception e) { |
544 |
> |
unexpectedException(); |
545 |
> |
} finally { |
546 |
> |
joinPool(p1); |
547 |
|
} |
478 |
– |
int max = 5; |
479 |
– |
if (tasks[4].cancel(true)) --max; |
480 |
– |
if (tasks[3].cancel(true)) --max; |
481 |
– |
p1.purge(); |
482 |
– |
long count = p1.getTaskCount(); |
483 |
– |
assertTrue(count > 0 && count <= max); |
484 |
– |
joinPool(p1); |
548 |
|
} |
549 |
|
|
550 |
|
/** |
554 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
555 |
|
for(int i = 0; i < 5; i++) |
556 |
|
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
557 |
< |
List l = p1.shutdownNow(); |
557 |
> |
List l; |
558 |
> |
try { |
559 |
> |
l = p1.shutdownNow(); |
560 |
> |
} catch (SecurityException ok) { |
561 |
> |
return; |
562 |
> |
} |
563 |
|
assertTrue(p1.isShutdown()); |
564 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
565 |
|
joinPool(p1); |
578 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
579 |
|
for(int i = 0; i < 5; i++) |
580 |
|
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
581 |
< |
p1.shutdown(); |
581 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
582 |
|
BlockingQueue q = p1.getQueue(); |
583 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
584 |
|
ScheduledFuture t = (ScheduledFuture)it.next(); |
609 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
610 |
|
for(int i = 0; i < 5; i++) |
611 |
|
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
612 |
< |
p1.shutdown(); |
612 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
613 |
|
assertTrue(p1.isShutdown()); |
614 |
|
BlockingQueue q = p1.getQueue(); |
615 |
|
assertTrue(q.isEmpty()); |
632 |
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
633 |
|
ScheduledFuture task = |
634 |
|
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
635 |
< |
p1.shutdown(); |
635 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
636 |
|
assertTrue(p1.isShutdown()); |
637 |
|
BlockingQueue q = p1.getQueue(); |
638 |
|
assertTrue(q.isEmpty()); |
653 |
|
try { |
654 |
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
655 |
|
ScheduledFuture task = |
656 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
656 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); |
657 |
|
assertFalse(task.isCancelled()); |
658 |
< |
p1.shutdown(); |
658 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
659 |
|
assertFalse(task.isCancelled()); |
660 |
|
assertFalse(p1.isTerminated()); |
661 |
|
assertTrue(p1.isShutdown()); |
662 |
|
Thread.sleep(SHORT_DELAY_MS); |
663 |
|
assertFalse(task.isCancelled()); |
664 |
< |
task.cancel(true); |
665 |
< |
assertTrue(task.isCancelled()); |
664 |
> |
assertTrue(task.cancel(true)); |
665 |
> |
assertTrue(task.isDone()); |
666 |
|
Thread.sleep(SHORT_DELAY_MS); |
667 |
|
assertTrue(p1.isTerminated()); |
668 |
|
} |
669 |
|
catch(Exception ex) { |
670 |
|
unexpectedException(); |
671 |
|
} |
672 |
< |
finally { |
673 |
< |
p1.shutdownNow(); |
672 |
> |
finally { |
673 |
> |
joinPool(p1); |
674 |
|
} |
675 |
|
} |
676 |
|
|
734 |
|
} |
735 |
|
} |
736 |
|
|
669 |
– |
|
670 |
– |
|
671 |
– |
|
672 |
– |
|
737 |
|
/** |
738 |
|
* invokeAny(null) throws NPE |
739 |
|
*/ |
1124 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1125 |
|
l.add(new StringTask()); |
1126 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1127 |
+ |
l.add(new StringTask()); |
1128 |
|
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1129 |
< |
assertEquals(2, result.size()); |
1129 |
> |
assertEquals(3, result.size()); |
1130 |
|
Iterator<Future<String>> it = result.iterator(); |
1131 |
|
Future<String> f1 = it.next(); |
1132 |
|
Future<String> f2 = it.next(); |
1133 |
+ |
Future<String> f3 = it.next(); |
1134 |
|
assertTrue(f1.isDone()); |
1069 |
– |
assertFalse(f1.isCancelled()); |
1135 |
|
assertTrue(f2.isDone()); |
1136 |
+ |
assertTrue(f3.isDone()); |
1137 |
+ |
assertFalse(f1.isCancelled()); |
1138 |
|
assertTrue(f2.isCancelled()); |
1139 |
|
} catch(Exception ex) { |
1140 |
|
unexpectedException(); |