1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
49 |
|
return other.pseudodelay == pseudodelay; |
50 |
|
} |
51 |
|
|
52 |
– |
|
52 |
|
public long getDelay(TimeUnit ignore) { |
53 |
|
return pseudodelay; |
54 |
|
} |
61 |
|
} |
62 |
|
} |
63 |
|
|
65 |
– |
|
64 |
|
/** |
65 |
|
* Delayed implementation that actually delays |
66 |
|
*/ |
102 |
|
} |
103 |
|
} |
104 |
|
|
107 |
– |
|
105 |
|
/** |
106 |
|
* Create a queue of given size containing consecutive |
107 |
|
* PDelays 0 ... n. |
108 |
|
*/ |
109 |
< |
private DelayQueue populatedQueue(int n) { |
110 |
< |
DelayQueue q = new DelayQueue(); |
109 |
> |
private DelayQueue<PDelay> populatedQueue(int n) { |
110 |
> |
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
111 |
|
assertTrue(q.isEmpty()); |
112 |
|
for (int i = n-1; i >= 0; i-=2) |
113 |
|
assertTrue(q.offer(new PDelay(i))); |
258 |
|
} catch (NullPointerException success) {} |
259 |
|
} |
260 |
|
|
264 |
– |
|
261 |
|
/** |
262 |
|
* addAll(this) throws IAE |
263 |
|
*/ |
314 |
|
/** |
315 |
|
* put(null) throws NPE |
316 |
|
*/ |
317 |
< |
public void testPutNull() { |
317 |
> |
public void testPutNull() { |
318 |
|
try { |
319 |
|
DelayQueue q = new DelayQueue(); |
320 |
|
q.put(null); |
321 |
|
shouldThrow(); |
322 |
|
} catch (NullPointerException success) {} |
323 |
< |
} |
323 |
> |
} |
324 |
|
|
325 |
|
/** |
326 |
|
* all elements successfully put are contained |
327 |
|
*/ |
328 |
< |
public void testPut() { |
329 |
< |
DelayQueue q = new DelayQueue(); |
330 |
< |
for (int i = 0; i < SIZE; ++i) { |
331 |
< |
PDelay I = new PDelay(i); |
332 |
< |
q.put(I); |
333 |
< |
assertTrue(q.contains(I)); |
334 |
< |
} |
335 |
< |
assertEquals(SIZE, q.size()); |
328 |
> |
public void testPut() { |
329 |
> |
DelayQueue q = new DelayQueue(); |
330 |
> |
for (int i = 0; i < SIZE; ++i) { |
331 |
> |
PDelay I = new PDelay(i); |
332 |
> |
q.put(I); |
333 |
> |
assertTrue(q.contains(I)); |
334 |
> |
} |
335 |
> |
assertEquals(SIZE, q.size()); |
336 |
|
} |
337 |
|
|
338 |
|
/** |
340 |
|
*/ |
341 |
|
public void testPutWithTake() throws InterruptedException { |
342 |
|
final DelayQueue q = new DelayQueue(); |
343 |
< |
Thread t = new Thread(new CheckedRunnable() { |
343 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
344 |
|
public void realRun() { |
345 |
|
q.put(new PDelay(0)); |
346 |
|
q.put(new PDelay(0)); |
348 |
|
q.put(new PDelay(0)); |
349 |
|
}}); |
350 |
|
|
351 |
< |
t.start(); |
352 |
< |
Thread.sleep(SHORT_DELAY_MS); |
357 |
< |
q.take(); |
358 |
< |
t.interrupt(); |
359 |
< |
t.join(); |
351 |
> |
awaitTermination(t); |
352 |
> |
assertEquals(4, q.size()); |
353 |
|
} |
354 |
|
|
355 |
|
/** |
357 |
|
*/ |
358 |
|
public void testTimedOffer() throws InterruptedException { |
359 |
|
final DelayQueue q = new DelayQueue(); |
360 |
< |
Thread t = new Thread(new CheckedRunnable() { |
360 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
361 |
|
public void realRun() throws InterruptedException { |
362 |
|
q.put(new PDelay(0)); |
363 |
|
q.put(new PDelay(0)); |
365 |
|
assertTrue(q.offer(new PDelay(0), LONG_DELAY_MS, MILLISECONDS)); |
366 |
|
}}); |
367 |
|
|
368 |
< |
t.start(); |
376 |
< |
Thread.sleep(SMALL_DELAY_MS); |
377 |
< |
t.interrupt(); |
378 |
< |
t.join(); |
368 |
> |
awaitTermination(t); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
387 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
388 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
389 |
|
public void realRun() { |
400 |
– |
long t0 = System.nanoTime(); |
390 |
|
threadStarted.countDown(); |
391 |
|
try { |
392 |
|
q.take(); |
393 |
|
shouldThrow(); |
394 |
< |
} catch (InterruptedException expected) {} |
395 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
394 |
> |
} catch (InterruptedException success) {} |
395 |
> |
assertFalse(Thread.interrupted()); |
396 |
|
}}); |
397 |
< |
threadStarted.await(); |
398 |
< |
Thread.sleep(SHORT_DELAY_MS); |
399 |
< |
assertTrue(t.isAlive()); |
397 |
> |
|
398 |
> |
await(threadStarted); |
399 |
> |
assertThreadStaysAlive(t); |
400 |
|
t.interrupt(); |
401 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
413 |
< |
assertFalse(t.isAlive()); |
401 |
> |
awaitTermination(t); |
402 |
|
} |
403 |
|
|
404 |
|
/** |
406 |
|
*/ |
407 |
|
public void testBlockingTake() throws InterruptedException { |
408 |
|
final DelayQueue q = populatedQueue(SIZE); |
409 |
< |
Thread t = new Thread(new CheckedRunnable() { |
409 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
410 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
411 |
|
public void realRun() throws InterruptedException { |
412 |
|
for (int i = 0; i < SIZE; ++i) { |
413 |
|
assertEquals(new PDelay(i), ((PDelay)q.take())); |
414 |
|
} |
415 |
+ |
|
416 |
+ |
Thread.currentThread().interrupt(); |
417 |
|
try { |
418 |
|
q.take(); |
419 |
|
shouldThrow(); |
420 |
|
} catch (InterruptedException success) {} |
421 |
+ |
assertFalse(Thread.interrupted()); |
422 |
+ |
|
423 |
+ |
pleaseInterrupt.countDown(); |
424 |
+ |
try { |
425 |
+ |
q.take(); |
426 |
+ |
shouldThrow(); |
427 |
+ |
} catch (InterruptedException success) {} |
428 |
+ |
assertFalse(Thread.interrupted()); |
429 |
|
}}); |
430 |
|
|
431 |
< |
t.start(); |
432 |
< |
Thread.sleep(SHORT_DELAY_MS); |
431 |
> |
await(pleaseInterrupt); |
432 |
> |
assertThreadStaysAlive(t); |
433 |
|
t.interrupt(); |
434 |
< |
t.join(); |
434 |
> |
awaitTermination(t); |
435 |
|
} |
436 |
|
|
438 |
– |
|
437 |
|
/** |
438 |
|
* poll succeeds unless empty |
439 |
|
*/ |
462 |
|
public void testTimedPoll() throws InterruptedException { |
463 |
|
DelayQueue q = populatedQueue(SIZE); |
464 |
|
for (int i = 0; i < SIZE; ++i) { |
465 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
466 |
< |
} |
467 |
< |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
465 |
> |
long startTime = System.nanoTime(); |
466 |
> |
assertEquals(new PDelay(i), ((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
467 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
468 |
> |
} |
469 |
> |
long startTime = System.nanoTime(); |
470 |
> |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
471 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
472 |
> |
checkEmpty(q); |
473 |
|
} |
474 |
|
|
475 |
|
/** |
477 |
|
* returning timeout status |
478 |
|
*/ |
479 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
480 |
< |
Thread t = new Thread(new CheckedRunnable() { |
480 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
481 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
482 |
|
public void realRun() throws InterruptedException { |
483 |
|
DelayQueue q = populatedQueue(SIZE); |
484 |
|
for (int i = 0; i < SIZE; ++i) { |
485 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
486 |
|
} |
487 |
+ |
|
488 |
+ |
Thread.currentThread().interrupt(); |
489 |
+ |
try { |
490 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
491 |
+ |
shouldThrow(); |
492 |
+ |
} catch (InterruptedException success) {} |
493 |
+ |
assertFalse(Thread.interrupted()); |
494 |
+ |
|
495 |
+ |
pleaseInterrupt.countDown(); |
496 |
|
try { |
497 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
497 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
498 |
|
shouldThrow(); |
499 |
|
} catch (InterruptedException success) {} |
500 |
+ |
assertFalse(Thread.interrupted()); |
501 |
|
}}); |
502 |
|
|
503 |
< |
t.start(); |
504 |
< |
Thread.sleep(SHORT_DELAY_MS); |
503 |
> |
await(pleaseInterrupt); |
504 |
> |
assertThreadStaysAlive(t); |
505 |
|
t.interrupt(); |
506 |
< |
t.join(); |
506 |
> |
awaitTermination(t); |
507 |
|
} |
508 |
|
|
509 |
|
/** |
514 |
|
final DelayQueue q = new DelayQueue(); |
515 |
|
final PDelay pdelay = new PDelay(0); |
516 |
|
final CheckedBarrier barrier = new CheckedBarrier(2); |
517 |
< |
Thread t = new Thread(new CheckedRunnable() { |
517 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
518 |
|
public void realRun() throws InterruptedException { |
519 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
520 |
|
|
521 |
|
barrier.await(); |
522 |
< |
assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
522 |
> |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
523 |
|
|
524 |
|
Thread.currentThread().interrupt(); |
525 |
|
try { |
526 |
< |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
526 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
527 |
|
shouldThrow(); |
528 |
|
} catch (InterruptedException success) {} |
529 |
+ |
assertFalse(Thread.interrupted()); |
530 |
|
|
531 |
|
barrier.await(); |
532 |
|
try { |
533 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
533 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
534 |
|
shouldThrow(); |
535 |
|
} catch (InterruptedException success) {} |
536 |
+ |
assertFalse(Thread.interrupted()); |
537 |
|
}}); |
538 |
|
|
523 |
– |
t.start(); |
539 |
|
barrier.await(); |
540 |
< |
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
540 |
> |
long startTime = System.nanoTime(); |
541 |
> |
assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS)); |
542 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
543 |
> |
|
544 |
|
barrier.await(); |
545 |
< |
sleep(SHORT_DELAY_MS); |
545 |
> |
assertThreadStaysAlive(t); |
546 |
|
t.interrupt(); |
547 |
< |
t.join(); |
547 |
> |
awaitTermination(t); |
548 |
|
} |
549 |
|
|
532 |
– |
|
550 |
|
/** |
551 |
|
* peek returns next element, or null if empty |
552 |
|
*/ |
700 |
|
* toArray(a) contains all elements |
701 |
|
*/ |
702 |
|
public void testToArray2() { |
703 |
< |
DelayQueue q = populatedQueue(SIZE); |
703 |
> |
DelayQueue<PDelay> q = populatedQueue(SIZE); |
704 |
|
PDelay[] ints = new PDelay[SIZE]; |
705 |
< |
assertSame(ints, q.toArray(ints)); |
705 |
> |
PDelay[] array = q.toArray(ints); |
706 |
> |
assertSame(ints, array); |
707 |
|
Arrays.sort(ints); |
708 |
|
for (int i = 0; i < ints.length; i++) |
709 |
|
assertSame(ints[i], q.remove()); |
710 |
|
} |
711 |
|
|
694 |
– |
|
712 |
|
/** |
713 |
|
* toArray(null) throws NullPointerException |
714 |
|
*/ |
762 |
|
assertFalse(it.hasNext()); |
763 |
|
} |
764 |
|
|
748 |
– |
|
765 |
|
/** |
766 |
|
* toString contains toStrings of elements |
767 |
|
*/ |
769 |
|
DelayQueue q = populatedQueue(SIZE); |
770 |
|
String s = q.toString(); |
771 |
|
for (int i = 0; i < SIZE; ++i) { |
772 |
< |
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
772 |
> |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
773 |
|
} |
774 |
|
} |
775 |
|
|
776 |
|
/** |
777 |
< |
* offer transfers elements across Executor tasks |
777 |
> |
* timed poll transfers elements across Executor tasks |
778 |
|
*/ |
779 |
|
public void testPollInExecutor() { |
780 |
|
final DelayQueue q = new DelayQueue(); |
781 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
782 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
783 |
|
executor.execute(new CheckedRunnable() { |
784 |
|
public void realRun() throws InterruptedException { |
785 |
|
assertNull(q.poll()); |
786 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
787 |
< |
assertTrue(q.isEmpty()); |
786 |
> |
threadsStarted.await(); |
787 |
> |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
788 |
> |
checkEmpty(q); |
789 |
|
}}); |
790 |
|
|
791 |
|
executor.execute(new CheckedRunnable() { |
792 |
|
public void realRun() throws InterruptedException { |
793 |
< |
Thread.sleep(SHORT_DELAY_MS); |
793 |
> |
threadsStarted.await(); |
794 |
|
q.put(new PDelay(1)); |
795 |
|
}}); |
796 |
|
|
797 |
|
joinPool(executor); |
798 |
|
} |
799 |
|
|
782 |
– |
|
800 |
|
/** |
801 |
|
* Delayed actions do not occur until their delay elapses |
802 |
|
*/ |
826 |
|
assertNotNull(q.peek()); |
827 |
|
} |
828 |
|
|
812 |
– |
|
829 |
|
/** |
830 |
|
* poll of a non-empty queue returns null if no expired elements. |
831 |
|
*/ |
948 |
|
} |
949 |
|
} |
950 |
|
|
935 |
– |
|
951 |
|
} |