94 |
|
} |
95 |
|
|
96 |
|
public boolean equals(Object other) { |
97 |
< |
return equals((NanoDelay)other); |
98 |
< |
} |
99 |
< |
public boolean equals(NanoDelay other) { |
100 |
< |
return other.trigger == trigger; |
97 |
> |
return (other instanceof NanoDelay) && |
98 |
> |
this.trigger == ((NanoDelay)other).trigger; |
99 |
|
} |
100 |
|
|
101 |
|
// suppress [overrides] javac warning |
117 |
|
|
118 |
|
/** |
119 |
|
* Returns a new queue of given size containing consecutive |
120 |
< |
* PDelays 0 ... n. |
120 |
> |
* PDelays 0 ... n - 1. |
121 |
|
*/ |
122 |
|
private DelayQueue<PDelay> populatedQueue(int n) { |
123 |
|
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
129 |
|
assertFalse(q.isEmpty()); |
130 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
131 |
|
assertEquals(n, q.size()); |
132 |
+ |
assertEquals(new PDelay(0), q.peek()); |
133 |
|
return q; |
134 |
|
} |
135 |
|
|
411 |
|
*/ |
412 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
413 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
414 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
415 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
416 |
|
public void realRun() throws InterruptedException { |
417 |
< |
DelayQueue q = populatedQueue(SIZE); |
417 |
> |
long startTime = System.nanoTime(); |
418 |
|
for (int i = 0; i < SIZE; ++i) { |
419 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
419 |
> |
assertEquals(new PDelay(i), |
420 |
> |
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
421 |
|
} |
422 |
|
|
423 |
|
Thread.currentThread().interrupt(); |
433 |
|
shouldThrow(); |
434 |
|
} catch (InterruptedException success) {} |
435 |
|
assertFalse(Thread.interrupted()); |
436 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
437 |
|
}}); |
438 |
|
|
439 |
|
await(pleaseInterrupt); |
440 |
|
assertThreadStaysAlive(t); |
441 |
|
t.interrupt(); |
442 |
|
awaitTermination(t); |
443 |
+ |
checkEmpty(q); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
656 |
|
public void testPollInExecutor() { |
657 |
|
final DelayQueue q = new DelayQueue(); |
658 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
659 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
660 |
< |
executor.execute(new CheckedRunnable() { |
661 |
< |
public void realRun() throws InterruptedException { |
662 |
< |
assertNull(q.poll()); |
663 |
< |
threadsStarted.await(); |
664 |
< |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
665 |
< |
checkEmpty(q); |
666 |
< |
}}); |
667 |
< |
|
668 |
< |
executor.execute(new CheckedRunnable() { |
669 |
< |
public void realRun() throws InterruptedException { |
670 |
< |
threadsStarted.await(); |
671 |
< |
q.put(new PDelay(1)); |
672 |
< |
}}); |
673 |
< |
|
674 |
< |
joinPool(executor); |
659 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
660 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
661 |
> |
executor.execute(new CheckedRunnable() { |
662 |
> |
public void realRun() throws InterruptedException { |
663 |
> |
assertNull(q.poll()); |
664 |
> |
threadsStarted.await(); |
665 |
> |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
666 |
> |
checkEmpty(q); |
667 |
> |
}}); |
668 |
> |
|
669 |
> |
executor.execute(new CheckedRunnable() { |
670 |
> |
public void realRun() throws InterruptedException { |
671 |
> |
threadsStarted.await(); |
672 |
> |
q.put(new PDelay(1)); |
673 |
> |
}}); |
674 |
> |
} |
675 |
|
} |
676 |
|
|
677 |
|
/** |