Java ArrayBlockingQueue add() Method

Published by user on

The add() method of ArrayBlockingQueue appends the specified element at the end of the queue. It returns true on the successful insertion of the element. It can throw below exceptions:

  • IllegalStateException – if the queue is full.
  • NullPointerException – if the specified element is null.

Syntax:

public boolean add(E e)

where e is the element to add to the tail of the queue.

Program to add the element to ArrayBlockingQueue

In the below example, we will see how to add elements to the ArrayBlockingQueue.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ArrayBlockingQueueAddExample {

	public static void main(String[] args) {

		int capacity = 5;
		BlockingQueue<Integer> salariesQue = new ArrayBlockingQueue<>(capacity);
		salariesQue.add(5988);
		salariesQue.add(5688);
		salariesQue.add(22233);
		salariesQue.add(44585);
		salariesQue.add(4555);

		System.out.println("Queue elements: \n" + salariesQue);

	}
}

Output:

Queue elements: 
[5988, 5688, 22233, 44585, 4555]

Example to show IllegalStateException during add

The below example shows how the add() method throws IllegalStateException if the queue is full.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class CapacityFullExample {

	public static void main(String[] args) {

		int capacity = 5;
		BlockingQueue<Integer> numbersQue = new ArrayBlockingQueue<>(capacity);
		numbersQue.add(8955);
		numbersQue.add(4545);
		numbersQue.add(2323);
		numbersQue.add(4545);
		numbersQue.add(8787845);

		System.out.println("BlockingQueue elements: \n" + numbersQue);

		numbersQue.add(89895); // throws IllegalStateException if the blocking
								// queue is full
	}
}

Output:

BlockingQueue elements: 
[8955, 4545, 2323, 4545, 8787845]
Exception in thread "main" java.lang.IllegalStateException: Queue full
	at java.util.AbstractQueue.add(AbstractQueue.java:98)
	at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312)
	at CapacityFullExample.main(CapacityFullExample.java:18)

Example 3:

The below example shows how the add method throws NullPointerException if the specified element is null.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class NullElementExample {
	public static void main(String[] args) {
		int capacity = 10;
		BlockingQueue<String> subjects = new ArrayBlockingQueue<>(capacity);
		subjects.add("Java");
		subjects.add("Spring");
		subjects.add(null);// adding null element gives NullPointerException
	}
}

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.concurrent.ArrayBlockingQueue.checkNotNull(ArrayBlockingQueue.java:150)
	at java.util.concurrent.ArrayBlockingQueue.offer(ArrayBlockingQueue.java:325)
	at java.util.AbstractQueue.add(AbstractQueue.java:95)
	at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312)
	at NullElementExample.main(NullElementExample.java:10)

Example 4:

The below example shows how to add user-defined objects into the queue.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class Employee {
	private int employeeId;
	private String employeeName;

	public Employee() {
	}

	public Employee(int employeeId, String employeeName) {
		super();
		this.employeeId = employeeId;
		this.employeeName = employeeName;
	}

	@Override
	public String toString() {
		return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + "]";
	}
}

public class AddUserDefinedDemo {

	public static void main(String[] args) {

		int capacity = 5;
		BlockingQueue<Employee>employees = new ArrayBlockingQueue<>(capacity);
		employees.add(new Employee(101, "John"));
		employees.add(new Employee(102, "Adam"));
		employees.add(new Employee(103, "Ricky"));
		employees.add(new Employee(104, "Shaun"));
		employees.add(new Employee(105, "Chris"));

		System.out.println("Queue elements: ");
		for (Employee employee :employees) {
			System.out.println(employee);
		}
	}
}

Output:

Queue elements: 
Employee [employeeId=101, employeeName=John]
Employee [employeeId=102, employeeName=Adam]
Employee [employeeId=103, employeeName=Ricky]
Employee [employeeId=104, employeeName=Shaun]
Employee [employeeId=105, employeeName=Chris]

As shown in the above example, we can add user-defined objects into the queue using add() method. It appends the element at the end of the queue.

 

That’s it for this article. Now you must have a good idea about how to add elements to ArrayBlockingQueue. Please share if you find this article helpful.

Categories: Java