251 |
} |
} |
252 |
|
|
253 |
/** |
/** |
254 |
* set in one thread causes get in another thread to retrieve value |
* run in one thread causes get in another thread to retrieve value |
255 |
*/ |
*/ |
256 |
public void testGet1() throws InterruptedException { |
public void testGetRun() throws InterruptedException { |
257 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
258 |
|
|
259 |
final FutureTask task = |
final FutureTask task = |
260 |
new FutureTask(new CheckedCallable<Object>() { |
new FutureTask(new CheckedCallable<Object>() { |
261 |
public Object realCall() throws InterruptedException { |
public Object realCall() throws InterruptedException { |
262 |
return Boolean.TRUE; |
return Boolean.TRUE; |
263 |
}}); |
}}); |
|
checkNotDone(task); |
|
264 |
|
|
265 |
Thread t = newStartedThread(new CheckedRunnable() { |
Thread t = newStartedThread(new CheckedRunnable() { |
266 |
public void realRun() throws Exception { |
public void realRun() throws Exception { |
267 |
|
threadStarted.countDown(); |
268 |
assertSame(Boolean.TRUE, task.get()); |
assertSame(Boolean.TRUE, task.get()); |
269 |
}}); |
}}); |
270 |
|
|
271 |
|
threadStarted.await(); |
272 |
|
checkNotDone(task); |
273 |
|
assertTrue(t.isAlive()); |
274 |
task.run(); |
task.run(); |
275 |
checkCompletedNormally(task, Boolean.TRUE); |
checkCompletedNormally(task, Boolean.TRUE); |
276 |
awaitTermination(t, MEDIUM_DELAY_MS); |
awaitTermination(t, MEDIUM_DELAY_MS); |
277 |
} |
} |
278 |
|
|
279 |
/** |
/** |
280 |
* set in one thread causes timed get in another thread to retrieve value |
* set in one thread causes get in another thread to retrieve value |
281 |
|
*/ |
282 |
|
public void testGetSet() throws InterruptedException { |
283 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
284 |
|
|
285 |
|
final PublicFutureTask task = |
286 |
|
new PublicFutureTask(new CheckedCallable<Object>() { |
287 |
|
public Object realCall() throws InterruptedException { |
288 |
|
return Boolean.TRUE; |
289 |
|
}}); |
290 |
|
|
291 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
292 |
|
public void realRun() throws Exception { |
293 |
|
threadStarted.countDown(); |
294 |
|
assertSame(Boolean.FALSE, task.get()); |
295 |
|
}}); |
296 |
|
|
297 |
|
threadStarted.await(); |
298 |
|
checkNotDone(task); |
299 |
|
assertTrue(t.isAlive()); |
300 |
|
task.set(Boolean.FALSE); |
301 |
|
checkCompletedNormally(task, Boolean.FALSE); |
302 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
|
* run in one thread causes timed get in another thread to retrieve value |
307 |
*/ |
*/ |
308 |
public void testTimedGet1() throws InterruptedException { |
public void testTimedGetRun() throws InterruptedException { |
309 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
310 |
|
|
311 |
final FutureTask task = |
final FutureTask task = |
312 |
new FutureTask(new CheckedCallable<Object>() { |
new FutureTask(new CheckedCallable<Object>() { |
313 |
public Object realCall() throws InterruptedException { |
public Object realCall() throws InterruptedException { |
314 |
return Boolean.TRUE; |
return Boolean.TRUE; |
315 |
}}); |
}}); |
|
checkNotDone(task); |
|
316 |
|
|
317 |
Thread t = newStartedThread(new CheckedRunnable() { |
Thread t = newStartedThread(new CheckedRunnable() { |
318 |
public void realRun() throws Exception { |
public void realRun() throws Exception { |
319 |
assertSame(Boolean.TRUE, task.get(SMALL_DELAY_MS, MILLISECONDS)); |
threadStarted.countDown(); |
320 |
|
assertSame(Boolean.TRUE, |
321 |
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
322 |
}}); |
}}); |
323 |
|
|
324 |
|
threadStarted.await(); |
325 |
|
checkNotDone(task); |
326 |
|
assertTrue(t.isAlive()); |
327 |
task.run(); |
task.run(); |
328 |
checkCompletedNormally(task, Boolean.TRUE); |
checkCompletedNormally(task, Boolean.TRUE); |
329 |
awaitTermination(t, MEDIUM_DELAY_MS); |
awaitTermination(t, MEDIUM_DELAY_MS); |
330 |
} |
} |
331 |
|
|
332 |
/** |
/** |
333 |
|
* set in one thread causes timed get in another thread to retrieve value |
334 |
|
*/ |
335 |
|
public void testTimedGetSet() throws InterruptedException { |
336 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
337 |
|
|
338 |
|
final PublicFutureTask task = |
339 |
|
new PublicFutureTask(new CheckedCallable<Object>() { |
340 |
|
public Object realCall() throws InterruptedException { |
341 |
|
return Boolean.TRUE; |
342 |
|
}}); |
343 |
|
|
344 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
345 |
|
public void realRun() throws Exception { |
346 |
|
threadStarted.countDown(); |
347 |
|
assertSame(Boolean.FALSE, |
348 |
|
task.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
349 |
|
}}); |
350 |
|
|
351 |
|
threadStarted.await(); |
352 |
|
checkNotDone(task); |
353 |
|
assertTrue(t.isAlive()); |
354 |
|
task.set(Boolean.FALSE); |
355 |
|
checkCompletedNormally(task, Boolean.FALSE); |
356 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
357 |
|
} |
358 |
|
|
359 |
|
/** |
360 |
* Cancelling a task causes timed get in another thread to throw |
* Cancelling a task causes timed get in another thread to throw |
361 |
* CancellationException |
* CancellationException |
362 |
*/ |
*/ |