Close

Java Collections - ArrayBlockingQueue Constructors 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

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]




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




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]




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

Caused by: java.lang.IllegalArgumentException
at java.base/java.util.concurrent.ArrayBlockingQueue.<init>(ArrayBlockingQueue.java:305)
at com.logicbig.example.arrayblockingqueue.ArrayBlockingQueueExample4.main(ArrayBlockingQueueExample4.java:16)
... 6 more




See Also