629 |
|
|
630 |
|
void checkToArray2(ArrayBlockingQueue q) { |
631 |
|
int size = q.size(); |
632 |
< |
Integer[] a1 = size == 0 ? null : new Integer[size-1]; |
632 |
> |
Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; |
633 |
|
Integer[] a2 = new Integer[size]; |
634 |
< |
Integer[] a3 = new Integer[size+2]; |
634 |
> |
Integer[] a3 = new Integer[size + 2]; |
635 |
|
if (size > 0) Arrays.fill(a1, 42); |
636 |
|
Arrays.fill(a2, 42); |
637 |
|
Arrays.fill(a3, 42); |
638 |
< |
Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); |
638 |
> |
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); |
639 |
|
Integer[] b2 = (Integer[]) q.toArray(a2); |
640 |
|
Integer[] b3 = (Integer[]) q.toArray(a3); |
641 |
|
assertSame(a2, b2); |
649 |
|
assertSame(b3[i], x); |
650 |
|
} |
651 |
|
assertNull(a3[size]); |
652 |
< |
assertEquals(42, (int) a3[size+1]); |
652 |
> |
assertEquals(42, (int) a3[size + 1]); |
653 |
|
if (size > 0) { |
654 |
|
assertNotSame(a1, b1); |
655 |
|
assertEquals(size, b1.length); |
788 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
789 |
|
q.add(one); |
790 |
|
q.add(two); |
791 |
– |
ExecutorService executor = Executors.newFixedThreadPool(2); |
791 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
792 |
< |
executor.execute(new CheckedRunnable() { |
793 |
< |
public void realRun() throws InterruptedException { |
794 |
< |
assertFalse(q.offer(three)); |
795 |
< |
threadsStarted.await(); |
796 |
< |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
797 |
< |
assertEquals(0, q.remainingCapacity()); |
798 |
< |
}}); |
799 |
< |
|
800 |
< |
executor.execute(new CheckedRunnable() { |
801 |
< |
public void realRun() throws InterruptedException { |
802 |
< |
threadsStarted.await(); |
803 |
< |
assertEquals(0, q.remainingCapacity()); |
804 |
< |
assertSame(one, q.take()); |
805 |
< |
}}); |
806 |
< |
|
807 |
< |
joinPool(executor); |
792 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
793 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
794 |
> |
executor.execute(new CheckedRunnable() { |
795 |
> |
public void realRun() throws InterruptedException { |
796 |
> |
assertFalse(q.offer(three)); |
797 |
> |
threadsStarted.await(); |
798 |
> |
assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); |
799 |
> |
assertEquals(0, q.remainingCapacity()); |
800 |
> |
}}); |
801 |
> |
|
802 |
> |
executor.execute(new CheckedRunnable() { |
803 |
> |
public void realRun() throws InterruptedException { |
804 |
> |
threadsStarted.await(); |
805 |
> |
assertEquals(0, q.remainingCapacity()); |
806 |
> |
assertSame(one, q.take()); |
807 |
> |
}}); |
808 |
> |
} |
809 |
|
} |
810 |
|
|
811 |
|
/** |
814 |
|
public void testPollInExecutor() { |
815 |
|
final ArrayBlockingQueue q = new ArrayBlockingQueue(2); |
816 |
|
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
817 |
< |
ExecutorService executor = Executors.newFixedThreadPool(2); |
818 |
< |
executor.execute(new CheckedRunnable() { |
819 |
< |
public void realRun() throws InterruptedException { |
820 |
< |
assertNull(q.poll()); |
821 |
< |
threadsStarted.await(); |
822 |
< |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
823 |
< |
checkEmpty(q); |
824 |
< |
}}); |
825 |
< |
|
826 |
< |
executor.execute(new CheckedRunnable() { |
827 |
< |
public void realRun() throws InterruptedException { |
828 |
< |
threadsStarted.await(); |
829 |
< |
q.put(one); |
830 |
< |
}}); |
831 |
< |
|
832 |
< |
joinPool(executor); |
817 |
> |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
818 |
> |
try (PoolCleaner cleaner = cleaner(executor)) { |
819 |
> |
executor.execute(new CheckedRunnable() { |
820 |
> |
public void realRun() throws InterruptedException { |
821 |
> |
assertNull(q.poll()); |
822 |
> |
threadsStarted.await(); |
823 |
> |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
824 |
> |
checkEmpty(q); |
825 |
> |
}}); |
826 |
> |
|
827 |
> |
executor.execute(new CheckedRunnable() { |
828 |
> |
public void realRun() throws InterruptedException { |
829 |
> |
threadsStarted.await(); |
830 |
> |
q.put(one); |
831 |
> |
}}); |
832 |
> |
} |
833 |
|
} |
834 |
|
|
835 |
|
/** |