48 |
|
|
49 |
|
/** |
50 |
|
* Returns a new queue of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
51 |
> |
* Integers 0 ... n - 1. |
52 |
|
*/ |
53 |
|
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
54 |
|
LinkedBlockingQueue<Integer> q = |
59 |
|
assertFalse(q.isEmpty()); |
60 |
|
assertEquals(0, q.remainingCapacity()); |
61 |
|
assertEquals(n, q.size()); |
62 |
+ |
assertEquals((Integer) 0, q.peek()); |
63 |
|
return q; |
64 |
|
} |
65 |
|
|
441 |
|
final CountDownLatch aboutToWait = new CountDownLatch(1); |
442 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
443 |
|
public void realRun() throws InterruptedException { |
444 |
+ |
long startTime = System.nanoTime(); |
445 |
|
for (int i = 0; i < SIZE; ++i) { |
444 |
– |
long t0 = System.nanoTime(); |
446 |
|
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
446 |
– |
assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS); |
447 |
|
} |
448 |
– |
long t0 = System.nanoTime(); |
448 |
|
aboutToWait.countDown(); |
449 |
|
try { |
450 |
< |
q.poll(MEDIUM_DELAY_MS, MILLISECONDS); |
450 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
451 |
|
shouldThrow(); |
452 |
|
} catch (InterruptedException success) { |
453 |
< |
assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS); |
453 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
454 |
|
} |
455 |
|
}}); |
456 |
|
|
457 |
< |
aboutToWait.await(); |
458 |
< |
waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); |
457 |
> |
await(aboutToWait); |
458 |
> |
waitForThreadToEnterWaitState(t); |
459 |
|
t.interrupt(); |
460 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
460 |
> |
awaitTermination(t); |
461 |
|
checkEmpty(q); |
462 |
|
} |
463 |
|
|
721 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
722 |
|
q.add(one); |
723 |
|
q.add(two); |
725 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
724 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
725 |
< |
executor.execute(new CheckedRunnable() { |
726 |
< |
public void realRun() throws InterruptedException { |
727 |
< |
assertFalse(q.offer(three)); |
728 |
< |
threadsStarted.await(); |
729 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
730 |
< |
assertEquals(0, q.remainingCapacity()); |
731 |
< |
}}); |
732 |
< |
|
733 |
< |
executor.execute(new CheckedRunnable() { |
734 |
< |
public void realRun() throws InterruptedException { |
735 |
< |
threadsStarted.await(); |
736 |
< |
assertSame(one, q.take()); |
737 |
< |
}}); |
738 |
< |
|
739 |
< |
joinPool(executor); |
725 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
726 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
727 |
> |
executor.execute(new CheckedRunnable() { |
728 |
> |
public void realRun() throws InterruptedException { |
729 |
> |
assertFalse(q.offer(three)); |
730 |
> |
threadsStarted.await(); |
731 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
732 |
> |
assertEquals(0, q.remainingCapacity()); |
733 |
> |
}}); |
734 |
> |
|
735 |
> |
executor.execute(new CheckedRunnable() { |
736 |
> |
public void realRun() throws InterruptedException { |
737 |
> |
threadsStarted.await(); |
738 |
> |
assertSame(one, q.take()); |
739 |
> |
}}); |
740 |
> |
} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
746 |
|
public void testPollInExecutor() { |
747 |
|
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
748 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
749 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
750 |
< |
executor.execute(new CheckedRunnable() { |
751 |
< |
public void realRun() throws InterruptedException { |
752 |
< |
assertNull(q.poll()); |
753 |
< |
threadsStarted.await(); |
754 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
755 |
< |
checkEmpty(q); |
756 |
< |
}}); |
757 |
< |
|
758 |
< |
executor.execute(new CheckedRunnable() { |
759 |
< |
public void realRun() throws InterruptedException { |
760 |
< |
threadsStarted.await(); |
761 |
< |
q.put(one); |
762 |
< |
}}); |
763 |
< |
|
764 |
< |
joinPool(executor); |
749 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
750 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
751 |
> |
executor.execute(new CheckedRunnable() { |
752 |
> |
public void realRun() throws InterruptedException { |
753 |
> |
assertNull(q.poll()); |
754 |
> |
threadsStarted.await(); |
755 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
756 |
> |
checkEmpty(q); |
757 |
> |
}}); |
758 |
> |
|
759 |
> |
executor.execute(new CheckedRunnable() { |
760 |
> |
public void realRun() throws InterruptedException { |
761 |
> |
threadsStarted.await(); |
762 |
> |
q.put(one); |
763 |
> |
}}); |
764 |
> |
} |
765 |
|
} |
766 |
|
|
767 |
|
/** |