181 |
|
* a newSingleThreadScheduledExecutor successfully runs delayed task |
182 |
|
*/ |
183 |
|
public void testNewSingleThreadScheduledExecutor() throws Exception { |
184 |
< |
TrackedCallable callable = new TrackedCallable(); |
185 |
< |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
186 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
187 |
< |
assertFalse(callable.done); |
188 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
189 |
< |
assertTrue(callable.done); |
190 |
< |
assertEquals(Boolean.TRUE, f.get()); |
191 |
< |
joinPool(p1); |
184 |
> |
ScheduledExecutorService p = Executors.newSingleThreadScheduledExecutor(); |
185 |
> |
try { |
186 |
> |
final CountDownLatch done = new CountDownLatch(1); |
187 |
> |
final Runnable task = new CheckedRunnable() { |
188 |
> |
public void realRun() { |
189 |
> |
done.countDown(); |
190 |
> |
}}; |
191 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
192 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
193 |
> |
assertFalse(f.isDone()); |
194 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
195 |
> |
assertTrue(f.isDone()); |
196 |
> |
assertEquals(Boolean.TRUE, f.get()); |
197 |
> |
} finally { |
198 |
> |
joinPool(p); |
199 |
> |
} |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
|
* a newScheduledThreadPool successfully runs delayed task |
204 |
|
*/ |
205 |
|
public void testnewScheduledThreadPool() throws Exception { |
206 |
< |
TrackedCallable callable = new TrackedCallable(); |
207 |
< |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
208 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
209 |
< |
assertFalse(callable.done); |
210 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
211 |
< |
assertTrue(callable.done); |
212 |
< |
assertEquals(Boolean.TRUE, f.get()); |
213 |
< |
joinPool(p1); |
206 |
> |
ScheduledExecutorService p = Executors.newScheduledThreadPool(2); |
207 |
> |
try { |
208 |
> |
final CountDownLatch done = new CountDownLatch(1); |
209 |
> |
final Runnable task = new CheckedRunnable() { |
210 |
> |
public void realRun() { |
211 |
> |
done.countDown(); |
212 |
> |
}}; |
213 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
214 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
215 |
> |
assertFalse(f.isDone()); |
216 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
217 |
> |
assertTrue(f.isDone()); |
218 |
> |
assertEquals(Boolean.TRUE, f.get()); |
219 |
> |
} finally { |
220 |
> |
joinPool(p); |
221 |
> |
} |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
|
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
226 |
|
*/ |
227 |
|
public void testunconfigurableScheduledExecutorService() throws Exception { |
228 |
< |
TrackedCallable callable = new TrackedCallable(); |
229 |
< |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
230 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
231 |
< |
assertFalse(callable.done); |
232 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
233 |
< |
assertTrue(callable.done); |
234 |
< |
assertEquals(Boolean.TRUE, f.get()); |
235 |
< |
joinPool(p1); |
228 |
> |
ScheduledExecutorService p = |
229 |
> |
Executors.unconfigurableScheduledExecutorService |
230 |
> |
(Executors.newScheduledThreadPool(2)); |
231 |
> |
try { |
232 |
> |
final CountDownLatch done = new CountDownLatch(1); |
233 |
> |
final Runnable task = new CheckedRunnable() { |
234 |
> |
public void realRun() { |
235 |
> |
done.countDown(); |
236 |
> |
}}; |
237 |
> |
Future f = p.schedule(Executors.callable(task, Boolean.TRUE), |
238 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
239 |
> |
assertFalse(f.isDone()); |
240 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
241 |
> |
assertTrue(f.isDone()); |
242 |
> |
assertEquals(Boolean.TRUE, f.get()); |
243 |
> |
} finally { |
244 |
> |
joinPool(p); |
245 |
> |
} |
246 |
|
} |
247 |
|
|
248 |
|
/** |