ArrayBlockingQueue add() method in Java
Last Updated :
14 May, 2019
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.
- ArrayBlockingQueue class is a member of the Java Collections Framework.
- Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
- The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
- If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.
The
add (E e) method inserts the element passed as a parameter to the method at the
tail of this queue. If adding the element exceeds the capacity of the queue then the method will throw an IllegalStateException. This method returns true if adding of element is successful else it will throw an IllegalStateException.
Syntax:
public boolean add(E e)
Parameter:
e - the element to be added to queue.
Return Value:
true if adding is successful.
Throws:
IllegalStateException - if this queue is full
NullPointerException - if the specified element is null
Example 1
Below program illustrate adding element to ArrayBlockingQueue.
Java
// Java Program to Demonstrate add(E e) method
// of ArrayBlockingQueue.
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of ArrayBlockingQueue
int capacity = 10;
// create object of ArrayBlockingQueue
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
// Add element to ArrayBlockingQueue
queue.add(23);
// print queue after add operation
System.out.println("After adding 23");
System.out.println(queue);
// add more numbers
queue.add(32);
queue.add(45);
queue.add(12);
// print queue after add operation
System.out.println("After adding 32, 45, 12");
System.out.println(queue);
// add more numbers
queue.add(27);
queue.add(67);
// print queue after add operation
System.out.println("After adding 27, 67");
System.out.println(queue);
}
}
Output :
After adding 23
[23]
After adding 32, 45, 12
[23, 32, 45, 12]
After adding 27, 67
[23, 32, 45, 12, 27, 67]
Example 2
Below program illustrate adding Element to ArrayBlockingQueue and exception thrown if queue is full.
Java
// Java Program to Demonstrate add(E e) method
// of ArrayBlockingQueue.
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of ArrayBlockingQueue to 5 elements
int capacity = 5;
// create object of ArrayBlockingQueue
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);
// Add 5 element to ArrayBlockingQueue
queue.add(23);
queue.add(32);
queue.add(45);
queue.add(12);
queue.add(27);
// print queue after add operation
System.out.println("After adding all 5 elements to queue");
System.out.println(queue);
// check whether queue is full or not.
if (queue.remainingCapacity() == 0) {
System.out.println("Queue is full");
}
else {
System.out.println("Queue is not full");
}
// try to add more elements
// If exception thrown print the exception.
try {
Boolean response = queue.add(27);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output :
After adding all 5 elements to queue
[23, 32, 45, 12, 27]
Queue is full
java.lang.IllegalStateException: Queue full
at java.util.AbstractQueue.add(Unknown Source)
at java.util.concurrent.ArrayBlockingQueue.add(Unknown Source)
at defaultpackage.GFG.main(GFG.java:38)
Reference:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#add(E)
Similar Reads
ArrayBlockingQueue Class in Java In Java, the ArrayBlockingQueue class is part of the java.util.concurrent package and implements the BlockingQueue interface. It is a thread-safe, bounded queue that helps manage producer-consumer scenarios by blocking threads when the queue is full or empty.The queue has a fixed size, specified dur
8 min read
ArrayBlockingQueue add() method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
3 min read
ArrayBlockingQueue clear() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue al
2 min read
ArrayBlockingQueue contains() method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
3 min read
ArrayBlockingQueue drainTo() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue al
5 min read
ArrayBlockingQueue iterator() Method in Java The iterator() method of ArrayBlockingQueue class is used to returns an iterator of the same elements as this queue in a proper sequence. The elements returned from this method contains elements in order from first(head) to last(tail). The returned iterator is weakly consistent. Syntax: public Itera
2 min read
ArrayBlockingQueue offer() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
6 min read
ArrayBlockingQueue peek() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework.Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.The queue al
2 min read
ArrayBlockingQueue poll() Method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
4 min read
ArrayBlockingQueue put() method in Java ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue
2 min read