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. |
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 success) {} |
395 |
< |
assertTrue(millisElapsedSince(t0) >= SHORT_DELAY_MS); |
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 |
|
*/ |
472 |
|
* returning timeout status |
473 |
|
*/ |
474 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
475 |
< |
Thread t = new Thread(new CheckedRunnable() { |
475 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
476 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
477 |
|
public void realRun() throws InterruptedException { |
478 |
|
DelayQueue q = populatedQueue(SIZE); |
479 |
|
for (int i = 0; i < SIZE; ++i) { |
480 |
|
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
481 |
|
} |
482 |
+ |
|
483 |
+ |
Thread.currentThread().interrupt(); |
484 |
+ |
try { |
485 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
486 |
+ |
shouldThrow(); |
487 |
+ |
} catch (InterruptedException success) {} |
488 |
+ |
assertFalse(Thread.interrupted()); |
489 |
+ |
|
490 |
+ |
pleaseInterrupt.countDown(); |
491 |
|
try { |
492 |
< |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
492 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
493 |
|
shouldThrow(); |
494 |
|
} catch (InterruptedException success) {} |
495 |
+ |
assertFalse(Thread.interrupted()); |
496 |
|
}}); |
497 |
|
|
498 |
< |
t.start(); |
499 |
< |
Thread.sleep(SHORT_DELAY_MS); |
498 |
> |
await(pleaseInterrupt); |
499 |
> |
assertThreadStaysAlive(t); |
500 |
|
t.interrupt(); |
501 |
< |
t.join(); |
501 |
> |
awaitTermination(t); |
502 |
|
} |
503 |
|
|
504 |
|
/** |
509 |
|
final DelayQueue q = new DelayQueue(); |
510 |
|
final PDelay pdelay = new PDelay(0); |
511 |
|
final CheckedBarrier barrier = new CheckedBarrier(2); |
512 |
< |
Thread t = new Thread(new CheckedRunnable() { |
512 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
513 |
|
public void realRun() throws InterruptedException { |
514 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
515 |
|
|
516 |
|
barrier.await(); |
517 |
< |
assertSame(pdelay, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
517 |
> |
assertSame(pdelay, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
518 |
|
|
519 |
|
Thread.currentThread().interrupt(); |
520 |
|
try { |
521 |
< |
q.poll(SHORT_DELAY_MS, MILLISECONDS); |
521 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
522 |
|
shouldThrow(); |
523 |
|
} catch (InterruptedException success) {} |
524 |
+ |
assertFalse(Thread.interrupted()); |
525 |
|
|
526 |
|
barrier.await(); |
527 |
|
try { |
528 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
528 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
529 |
|
shouldThrow(); |
530 |
|
} catch (InterruptedException success) {} |
531 |
+ |
assertFalse(Thread.interrupted()); |
532 |
|
}}); |
533 |
|
|
523 |
– |
t.start(); |
534 |
|
barrier.await(); |
535 |
< |
assertTrue(q.offer(pdelay, SHORT_DELAY_MS, MILLISECONDS)); |
535 |
> |
long startTime = System.nanoTime(); |
536 |
> |
assertTrue(q.offer(pdelay, LONG_DELAY_MS, MILLISECONDS)); |
537 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
538 |
> |
|
539 |
|
barrier.await(); |
540 |
< |
sleep(SHORT_DELAY_MS); |
540 |
> |
assertThreadStaysAlive(t); |
541 |
|
t.interrupt(); |
542 |
< |
t.join(); |
542 |
> |
awaitTermination(t); |
543 |
|
} |
544 |
|
|
532 |
– |
|
545 |
|
/** |
546 |
|
* peek returns next element, or null if empty |
547 |
|
*/ |
704 |
|
assertSame(ints[i], q.remove()); |
705 |
|
} |
706 |
|
|
695 |
– |
|
707 |
|
/** |
708 |
|
* toArray(null) throws NullPointerException |
709 |
|
*/ |
757 |
|
assertFalse(it.hasNext()); |
758 |
|
} |
759 |
|
|
749 |
– |
|
760 |
|
/** |
761 |
|
* toString contains toStrings of elements |
762 |
|
*/ |
764 |
|
DelayQueue q = populatedQueue(SIZE); |
765 |
|
String s = q.toString(); |
766 |
|
for (int i = 0; i < SIZE; ++i) { |
767 |
< |
assertTrue(s.indexOf(String.valueOf(Integer.MIN_VALUE+i)) >= 0); |
767 |
> |
assertTrue(s.contains(String.valueOf(Integer.MIN_VALUE+i))); |
768 |
|
} |
769 |
|
} |
770 |
|
|
771 |
|
/** |
772 |
< |
* offer transfers elements across Executor tasks |
772 |
> |
* timed poll transfers elements across Executor tasks |
773 |
|
*/ |
774 |
|
public void testPollInExecutor() { |
775 |
|
final DelayQueue q = new DelayQueue(); |
776 |
+ |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
777 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
778 |
|
executor.execute(new CheckedRunnable() { |
779 |
|
public void realRun() throws InterruptedException { |
780 |
|
assertNull(q.poll()); |
781 |
< |
assertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
782 |
< |
assertTrue(q.isEmpty()); |
781 |
> |
threadsStarted.await(); |
782 |
> |
assertTrue(null != q.poll(LONG_DELAY_MS, MILLISECONDS)); |
783 |
> |
checkEmpty(q); |
784 |
|
}}); |
785 |
|
|
786 |
|
executor.execute(new CheckedRunnable() { |
787 |
|
public void realRun() throws InterruptedException { |
788 |
< |
Thread.sleep(SHORT_DELAY_MS); |
788 |
> |
threadsStarted.await(); |
789 |
|
q.put(new PDelay(1)); |
790 |
|
}}); |
791 |
|
|
792 |
|
joinPool(executor); |
793 |
|
} |
794 |
|
|
783 |
– |
|
795 |
|
/** |
796 |
|
* Delayed actions do not occur until their delay elapses |
797 |
|
*/ |
821 |
|
assertNotNull(q.peek()); |
822 |
|
} |
823 |
|
|
813 |
– |
|
824 |
|
/** |
825 |
|
* poll of a non-empty queue returns null if no expired elements. |
826 |
|
*/ |
943 |
|
} |
944 |
|
} |
945 |
|
|
936 |
– |
|
946 |
|
} |