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) { |
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 |
|
*/ |
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() { |
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 |
|
} |
479 |
– |
int max = 5; |
480 |
– |
if (tasks[4].cancel(true)) --max; |
481 |
– |
if (tasks[3].cancel(true)) --max; |
482 |
– |
p1.purge(); |
483 |
– |
long count = p1.getTaskCount(); |
484 |
– |
assertTrue(count > 0 && count <= max); |
485 |
– |
joinPool(p1); |
548 |
|
} |
549 |
|
|
550 |
|
/** |
734 |
|
} |
735 |
|
} |
736 |
|
|
675 |
– |
|
676 |
– |
|
677 |
– |
|
678 |
– |
|
737 |
|
/** |
738 |
|
* invokeAny(null) throws NPE |
739 |
|
*/ |