Java ArrayBlockingQueue offer() Method

Published by user on

The offer() method in ArrayBlockingQueue appends the specified element at the end of the queue if the queue is not full. It can wait until the specified waiting time for space to get free and then inserts the element.

It returns true on successful insertion otherwise returns false. The method throws NullPointerException if the element is null.

If we use wait time with the offer() method, then it can throw InterruptedException if the method gets disrupted while waiting.

Syntax:

public boolean offer(E e)

Parameter – e is the element to add.

public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException

Parameters – e is the element to add,
timeout is the waiting time for space to get free if the queue is full,
unit is the time unit like seconds, milliseconds, microseconds, etc.

Program to Show ArrayBlockingQueue Offer Method

Let’s have a look at the program.

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

public class ArrayBlockingQueueOfferExample {

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

		int capacity = 5;
		BlockingQueue<Integer> numberQue = new ArrayBlockingQueue<>(capacity);
		numberQue.offer(10000);
		numberQue.offer(8000);
		numberQue.offer(11000);
		numberQue.offer(125000);
		numberQue.offer(8900);
		numberQue.offer(27000, 100, TimeUnit.MILLISECONDS);

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

Output:

Queue elements: 
[10000, 8000, 11000, 125000, 8900]

As shown in the above example, we can insert elements at the end of the queue using the offer() method. JVM can wait for 100 milliseconds to get free space in the queue to insert value 27000. As we are using waiting time with the offer method, It can throw InterruptedException. So we need to declare InterruptedException at the method declaration level or wrap offer() method inside the try-catch block to handle the exception.

Example with null Element

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

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

public class OfferWithNullDemo {

	public static void main(String[] args) {

		int capacity = 5;
		BlockingQueue<String> queue = new ArrayBlockingQueue<>(capacity);
		queue.offer(null);// adding null element throws 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 OfferWithNullDemo.main(OfferWithNullDemo.java:10)

User-defined Object Example

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

public class OfferWithCustomObject {
	public static void main(String[] args) {
		int capacity = 5;
		BlockingQueue<Employee> employees = new ArrayBlockingQueue<>(capacity);
		employees.offer(new Employee(101, "John"));
		employees.offer(new Employee(102, "Adam"));
		employees.offer(new Employee(103, "Ricky"));
		employees.offer(new Employee(104, "Shaun"));
		employees.offer(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]

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

Categories: Java