Close

Java Collections - ArrayBlockingQueue Constructors Examples

[Last Updated: Dec 10, 2025]

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

Constructors:

public ArrayBlockingQueue(int capacity)

Creates an ArrayBlockingQueue with the given (fixed) capacity and default access policy.



public ArrayBlockingQueue(int capacity,
                          boolean fair)

Creates an ArrayBlockingQueue with the given (fixed) capacity and the specified access policy.



public ArrayBlockingQueue(int capacity,
                          boolean fair,
                          Collection<? extends E> c)

Creates an ArrayBlockingQueue with the given (fixed) capacity, the specified access policy and initially containing the elements of the given collection, added in traversal order of the collection's iterator.


Examples


package com.logicbig.example.arrayblockingqueue;

import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueueExample {

public static void main(String... args) throws InterruptedException {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(3);
q.add(1);
q.add(2);
q.add(3);
System.out.println(q);
}
}

Output

[1, 2, 3]
JDK 25




package com.logicbig.example.arrayblockingqueue;

import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueueExample2 {

public static void main(String... args) throws InterruptedException {
ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(1, true);
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
q.put(i);
} catch (InterruptedException e) {
System.err.println(e);
}
}
}).start();

new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.err.println(e);
}
for (int i = 0; i < 5; i++) {
try {
System.out.println(q.take());
} catch (InterruptedException e) {
System.err.println(e);
}
}
}).start();
}
}

Output

0
1
2
3
4
JDK 25




package com.logicbig.example.arrayblockingqueue;

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

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

Output

[1, 2, 3]
JDK 25




Trying to add more items than the capacity will throw IllegalArgumentException:

package com.logicbig.example.arrayblockingqueue;

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

public class ArrayBlockingQueueExample4 {

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

Output

java.lang.IllegalArgumentException
at java.util.concurrent.ArrayBlockingQueue.<init> (ArrayBlockingQueue.java:309)
at com.logicbig.example.arrayblockingqueue.ArrayBlockingQueueExample4.main (ArrayBlockingQueueExample4.java:16)
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:104)
at java.lang.reflect.Method.invoke (Method.java:565)
at org.codehaus.mojo.exec.AbstractExecJavaBase.executeMainMethod (AbstractExecJavaBase.java:402)
at org.codehaus.mojo.exec.ExecJavaMojo.executeMainMethod (ExecJavaMojo.java:142)
at org.codehaus.mojo.exec.AbstractExecJavaBase.doExecClassLoader (AbstractExecJavaBase.java:377)
at org.codehaus.mojo.exec.AbstractExecJavaBase.lambda$execute$0 (AbstractExecJavaBase.java:287)
at java.lang.Thread.run (Thread.java:1474)
JDK 25




See Also