10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
< |
import java.util.concurrent.atomic.*; |
13 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
14 |
|
|
15 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
141 |
|
*/ |
142 |
|
public void testFixedRateSequence() throws InterruptedException { |
143 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
144 |
< |
RunnableCounter counter = new RunnableCounter(); |
145 |
< |
ScheduledFuture h = |
146 |
< |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
147 |
< |
delay(SMALL_DELAY_MS); |
148 |
< |
h.cancel(true); |
149 |
< |
int c = counter.count.get(); |
150 |
< |
// By time scaling conventions, we must have at least |
151 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
152 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
153 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
154 |
< |
joinPool(p); |
144 |
> |
try { |
145 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
146 |
> |
long startTime = System.nanoTime(); |
147 |
> |
int cycles = 10; |
148 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
149 |
> |
CheckedRunnable task = new CheckedRunnable() { |
150 |
> |
public void realRun() { done.countDown(); }}; |
151 |
> |
ScheduledFuture h = |
152 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
153 |
> |
done.await(); |
154 |
> |
h.cancel(true); |
155 |
> |
double normalizedTime = |
156 |
> |
(double) millisElapsedSince(startTime) / delay; |
157 |
> |
if (normalizedTime >= cycles - 1 && |
158 |
> |
normalizedTime <= cycles) |
159 |
> |
return; |
160 |
> |
} |
161 |
> |
throw new AssertionError("unexpected execution rate"); |
162 |
> |
} finally { |
163 |
> |
joinPool(p); |
164 |
> |
} |
165 |
|
} |
166 |
|
|
167 |
|
/** |
169 |
|
*/ |
170 |
|
public void testFixedDelaySequence() throws InterruptedException { |
171 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
172 |
< |
RunnableCounter counter = new RunnableCounter(); |
173 |
< |
ScheduledFuture h = |
174 |
< |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
175 |
< |
delay(SMALL_DELAY_MS); |
176 |
< |
h.cancel(true); |
177 |
< |
int c = counter.count.get(); |
178 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
179 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
180 |
< |
joinPool(p); |
172 |
> |
try { |
173 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
174 |
> |
long startTime = System.nanoTime(); |
175 |
> |
int cycles = 10; |
176 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
177 |
> |
CheckedRunnable task = new CheckedRunnable() { |
178 |
> |
public void realRun() { done.countDown(); }}; |
179 |
> |
ScheduledFuture h = |
180 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
181 |
> |
done.await(); |
182 |
> |
h.cancel(true); |
183 |
> |
double normalizedTime = |
184 |
> |
(double) millisElapsedSince(startTime) / delay; |
185 |
> |
if (normalizedTime >= cycles - 1 && |
186 |
> |
normalizedTime <= cycles) |
187 |
> |
return; |
188 |
> |
} |
189 |
> |
throw new AssertionError("unexpected execution rate"); |
190 |
> |
} finally { |
191 |
> |
joinPool(p); |
192 |
> |
} |
193 |
|
} |
194 |
|
|
195 |
|
/** |
342 |
|
threadProceed.await(); |
343 |
|
threadDone.countDown(); |
344 |
|
}}); |
345 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
345 |
> |
await(threadStarted); |
346 |
|
assertEquals(0, p.getCompletedTaskCount()); |
347 |
|
threadProceed.countDown(); |
348 |
|
threadDone.await(); |
1177 |
|
l.add(new StringTask()); |
1178 |
|
List<Future<String>> futures = |
1179 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1180 |
< |
assertEquals(3, futures.size()); |
1181 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1182 |
< |
Future<String> f1 = it.next(); |
1183 |
< |
Future<String> f2 = it.next(); |
1184 |
< |
Future<String> f3 = it.next(); |
1163 |
< |
assertTrue(f1.isDone()); |
1164 |
< |
assertTrue(f2.isDone()); |
1165 |
< |
assertTrue(f3.isDone()); |
1166 |
< |
assertFalse(f1.isCancelled()); |
1167 |
< |
assertTrue(f2.isCancelled()); |
1180 |
> |
assertEquals(l.size(), futures.size()); |
1181 |
> |
for (Future future : futures) |
1182 |
> |
assertTrue(future.isDone()); |
1183 |
> |
assertFalse(futures.get(0).isCancelled()); |
1184 |
> |
assertTrue(futures.get(1).isCancelled()); |
1185 |
|
} finally { |
1186 |
|
joinPool(e); |
1187 |
|
} |