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 |
122 |
|
private DelayQueue<PDelay> populatedQueue(int n) { |
123 |
|
DelayQueue<PDelay> q = new DelayQueue<PDelay>(); |
124 |
|
assertTrue(q.isEmpty()); |
125 |
< |
for (int i = n-1; i >= 0; i -= 2) |
125 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
126 |
|
assertTrue(q.offer(new PDelay(i))); |
127 |
|
for (int i = (n & 1); i < n; i += 2) |
128 |
|
assertTrue(q.offer(new PDelay(i))); |
410 |
|
*/ |
411 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
412 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
413 |
+ |
final DelayQueue q = populatedQueue(SIZE); |
414 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
415 |
|
public void realRun() throws InterruptedException { |
416 |
< |
DelayQueue q = populatedQueue(SIZE); |
416 |
> |
long startTime = System.nanoTime(); |
417 |
|
for (int i = 0; i < SIZE; ++i) { |
418 |
< |
assertEquals(new PDelay(i), ((PDelay)q.poll(SHORT_DELAY_MS, MILLISECONDS))); |
418 |
> |
assertEquals(new PDelay(i), |
419 |
> |
((PDelay)q.poll(LONG_DELAY_MS, MILLISECONDS))); |
420 |
|
} |
421 |
|
|
422 |
|
Thread.currentThread().interrupt(); |
432 |
|
shouldThrow(); |
433 |
|
} catch (InterruptedException success) {} |
434 |
|
assertFalse(Thread.interrupted()); |
435 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
436 |
|
}}); |
437 |
|
|
438 |
|
await(pleaseInterrupt); |
439 |
|
assertThreadStaysAlive(t); |
440 |
|
t.interrupt(); |
441 |
|
awaitTermination(t); |
442 |
+ |
checkEmpty(q); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
655 |
|
public void testPollInExecutor() { |
656 |
|
final DelayQueue q = new DelayQueue(); |
657 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
658 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
659 |
< |
executor.execute(new CheckedRunnable() { |
660 |
< |
public void realRun() throws InterruptedException { |
661 |
< |
assertNull(q.poll()); |
662 |
< |
threadsStarted.await(); |
663 |
< |
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS)); |
664 |
< |
checkEmpty(q); |
665 |
< |
}}); |
666 |
< |
|
667 |
< |
executor.execute(new CheckedRunnable() { |
668 |
< |
public void realRun() throws InterruptedException { |
669 |
< |
threadsStarted.await(); |
670 |
< |
q.put(new PDelay(1)); |
671 |
< |
}}); |
672 |
< |
|
673 |
< |
joinPool(executor); |
658 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
659 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
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 |
|
} |
675 |
|
|
676 |
|
/** |