Close

Java Collections - ArrayBlockingQueue.addAll() Examples

Java Collections Java Java API 


Class:

java.util.concurrent.ArrayBlockingQueue

java.lang.Objectjava.lang.Objectjava.util.AbstractCollectionjava.util.AbstractCollectionjava.util.CollectionCollectionjava.util.AbstractQueuejava.util.AbstractQueuejava.util.QueueQueuejava.util.concurrent.ArrayBlockingQueuejava.util.concurrent.ArrayBlockingQueuejava.util.concurrent.BlockingQueueBlockingQueuejava.io.SerializableSerializableLogicBig

Method:

public boolean addAll(Collection<? extends E> c)

Adds all of the elements in the specified collection to this queue.


Examples


package com.logicbig.example.arrayblockingqueue;

import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;

public class AddAllExample {

public static void main(String... args) {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3);
q.addAll(Arrays.asList(1, 2, 3));
System.out.println(q);
}
}

Output

[1, 2, 3]




package com.logicbig.example.arrayblockingqueue;

import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;

public class AddAllExample2 {

public static void main(String... args) {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(2);
q.addAll(Arrays.asList(1, 2, 3));
System.out.println(q);
}
}

Output

Caused by: java.lang.IllegalStateException: Queue full
at java.base/java.util.AbstractQueue.add(AbstractQueue.java:98)
at java.base/java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:326)
at java.base/java.util.AbstractQueue.addAll(AbstractQueue.java:187)
at com.logicbig.example.arrayblockingqueue.AddAllExample2.main(AddAllExample2.java:15)
... 6 more




See Also