下面列出了java.util.concurrent.PriorityBlockingQueue#addAll ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void whenPollingEmptyQueue_thenShouldBlockThread() throws InterruptedException {
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
final Thread thread = new Thread(() -> {
LOG.debug("Polling...");
while (true) {
try {
Integer poll = queue.take();
LOG.debug("Polled: " + poll);
} catch (InterruptedException ignored) {
}
}
});
thread.start();
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
LOG.debug("Adding to queue");
queue.addAll(newArrayList(1, 5, 6, 1, 2, 6, 7));
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
}
/**
* The comparator used in constructor is used
*/
public void testConstructor7() {
MyReverseComparator cmp = new MyReverseComparator();
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
assertEquals(cmp, q.comparator());
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
for (int i = SIZE - 1; i >= 0; --i)
assertEquals(ints[i], q.poll());
}
/**
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
PriorityBlockingQueue q = populatedQueue(SIZE);
try {
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* addAll of a collection with any null elements throws NPE after
* possibly adding some elements
*/
public void testAddAll3() {
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
}
/**
* The comparator used in constructor is used
*/
public void testConstructor7() {
MyReverseComparator cmp = new MyReverseComparator();
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
assertEquals(cmp, q.comparator());
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE; ++i)
ints[i] = new Integer(i);
q.addAll(Arrays.asList(ints));
for (int i = SIZE - 1; i >= 0; --i)
assertEquals(ints[i], q.poll());
}
/**
* addAll(this) throws IAE
*/
public void testAddAllSelf() {
PriorityBlockingQueue q = populatedQueue(SIZE);
try {
q.addAll(q);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
/**
* addAll of a collection with any null elements throws NPE after
* possibly adding some elements
*/
public void testAddAll3() {
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
Integer[] ints = new Integer[SIZE];
for (int i = 0; i < SIZE - 1; ++i)
ints[i] = new Integer(i);
try {
q.addAll(Arrays.asList(ints));
shouldThrow();
} catch (NullPointerException success) {}
}