Press "Enter" to skip to content

Java ArrayBlockingQueue put() Method

The put() method of ArrayBlockingQueue class appends the specified element at the tail of the queue if the queue is not full. If the queue is full, then it waits for space to become available. It throws below exceptions:

  • InterruptedException – if interrupted while waiting.
  • NullPointerException – if the specified element is null.

Syntax:

void put(E e) throws InterruptedException

parameter – e is the element to add to the queue.

Freshers Placement

As this method throws InterruptedException, we need to throw an exception from the method signature or wrap the put method inside a try-catch block.

Program to demonstrate put method

Let’s look at the program to use put() to insert the element into the queue.

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

public class ArrayBlockingQueuePutExample {

	public static void main(String[] args) throws InterruptedException {

		int capacity = 5;
		BlockingQueue<Integer> salQue = new ArrayBlockingQueue<>(capacity);
		salQue.put(25000);
		salQue.put(40000);
		salQue.put(59000);
		salQue.put(20000);
		salQue.put(8590);

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

Output:

Queue elements: 
[25000, 40000, 59000, 20000, 8590]

As shown in the above example, the put() method adds the elements at the end of the queue. Also, the main method throws InterruptedException exception if it gets interrupted while waiting.

Example 2:

Let’s see how to use put() to add user-defined objects in ArrayBlockingQueue.

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 + "]";
	}
}

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

public class PutUserDefinedDemo {

	public static void main(String[] args) throws InterruptedException {

		int capacity = 5;
		BlockingQueue<Employee> employees = new ArrayBlockingQueue<>(capacity);
		employees.put(new Employee(101, "John"));
		employees.put(new Employee(102, "Adam"));
		employees.put(new Employee(103, "Ricky"));
		employees.put(new Employee(104, "Shaun"));
		employees.put(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]

Null element with ArrayBlockingQueue put method

Freshers Placement

Let’s look at the example where the put() method throws NullPointerException.

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

public class PutNullDemo {

	public static void main(String[] args) throws InterruptedException {

		int capacity = 5;
		BlockingQueue<Integer> numbers = new ArrayBlockingQueue<>(capacity);
		numbers.put(10000);
		numbers.put(20000);
		numbers.put(30000);

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

		numbers.put(null);// throws NullPointerException for null values
	}
}

Output:

Queue elements: 
[10000, 20000, 30000]
Exception in thread "main" java.lang.NullPointerException
	at java.util.concurrent.ArrayBlockingQueue.checkNotNull(ArrayBlockingQueue.java:150)
	at java.util.concurrent.ArrayBlockingQueue.put(ArrayBlockingQueue.java:348)
	at PutNullDemo.main(PutNullDemo.java:16)

That’s all about put method in ArrayBlockingQueue. Please share if you find this article helpful.

Comments are closed.