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 |
+ |
assertTrue(h.isDone()); |
148 |
+ |
joinPool(p1); |
149 |
+ |
} catch(Exception e){ |
150 |
+ |
unexpectedException(); |
151 |
+ |
} |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
/** |
155 |
+ |
* scheduleWithFixedDelay executes series of tasks with given period |
156 |
+ |
*/ |
157 |
+ |
public void testFixedDelaySequence() { |
158 |
+ |
try { |
159 |
+ |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
160 |
+ |
RunnableCounter counter = new RunnableCounter(); |
161 |
+ |
ScheduledFuture h = |
162 |
+ |
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
163 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
164 |
+ |
h.cancel(true); |
165 |
+ |
int c = counter.count.get(); |
166 |
+ |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
167 |
+ |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
168 |
+ |
assertTrue(h.isDone()); |
169 |
+ |
joinPool(p1); |
170 |
+ |
} catch(Exception e){ |
171 |
+ |
unexpectedException(); |
172 |
+ |
} |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
|
176 |
+ |
/** |
177 |
|
* execute (null) throws NPE |
178 |
|
*/ |
179 |
|
public void testExecuteNull() { |
723 |
|
} |
724 |
|
} |
725 |
|
|
675 |
– |
|
676 |
– |
|
677 |
– |
|
678 |
– |
|
726 |
|
/** |
727 |
|
* invokeAny(null) throws NPE |
728 |
|
*/ |