Last Updated : 11 Jul, 2025
ArrayBlockingQueueis bounded, blocking queue that stores the elements internally backed by an array.
There are two types of offer() method depending upon parameter passed to it:
public boolean offer(E e)Parameter: The method takes one parameter, element. This refers to the element to be added in the queue. Return Value: This method returns True when the addition operation is successful and false if this queue is full. Exception The method throws NullPointerException if the specified element is null. Below programs illustrate offer(E element) method of ArrayBlockingQueue: Program 1: Java
// Demonstrating offer(E element) method of ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args) {
// Define capacity of ArrayBlockingQueue
int capacity = 5;
// Create object of ArrayBlockingQueue
ArrayBlockingQueue<Integer> queue =
new ArrayBlockingQueue<Integer>(capacity);
// Add 5 elements to ArrayBlockingQueue
System.out.println("adding 423: "+queue.offer(423));
System.out.println("adding 243: "+queue.offer(243));
System.out.println("adding 237: "+queue.offer(237));
System.out.println("adding 867: "+queue.offer(867));
System.out.println("adding 23: "+queue.offer(23));
// Check whether queue is full or not
if(queue.remainingCapacity()==0) {
System.out.println("queue is full");
System.out.println("queue contains "+queue);
}else {
System.out.println("queue is not full");
System.out.println("queue contains "+queue);
}
// Try to add more elements
System.out.println("adding 123: "+queue.offer(123));
System.out.println("adding 321: "+queue.offer(321));
}
}
Output:
adding 423: true adding 243: true adding 237: true adding 867: true adding 23: true queue is full queue contains [423, 243, 237, 867, 23] adding 123: false adding 321: falseProgram 2: Java
// Program Demonstrate offer(E e) method of ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
// create a User Object with name and age as an attribute
public class User {
public String name;
public String age;
User(String name, String age)
{
this.name = name;
this.age = age;
}
}
// Main Method
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.offerExample();
}
// Method to give example of offer function
public void offerExample()
{
// Define the capacity of ArrayBlockingQueue
int capacity = 5;
// Create object of ArrayBlockingQueue
ArrayBlockingQueue<User> queue =
new ArrayBlockingQueue<User>(capacity);
// Create user objects
User user1 = new User("Aman", "24");
User user2 = new User("Amar", "23");
User user3 = new User("Sanjeet", "25");
User user4 = new User("Suvo", "26");
User user5 = new User("Ravi", "22");
// Add Objects to ArrayBlockingQueue
System.out.println("adding user having name = "
+ user1.name + ": " + queue.offer(user1));
System.out.println("adding user having name = "
+ user2.name + ": " + queue.offer(user2));
System.out.println("adding user having name = "
+ user3.name + ": " + queue.offer(user3));
System.out.println("adding user having name = "
+ user4.name + ": " + queue.offer(user4));
System.out.println("adding user having name = "
+ user5.name + ": " + queue.offer(user5));
// Check whether the queue is full or not
if (queue.remainingCapacity() == 0) {
System.out.println("queue is full");
}
else {
System.out.println("queue is not full");
}
// Create more user objects
User user6 = new User("Ram", "20");
User user7 = new User("Mohan", "27");
// Add users in queue
System.out.println("adding user having name = "
+ user6.name + ": " + queue.offer(user6));
System.out.println("adding user having name = "
+ user7.name + ": " + queue.offer(user7));
}
}
Output:
adding user having name = Aman: true adding user having name = Amar: true adding user having name = Sanjeet: true adding user having name = Suvo: true adding user having name = Ravi: true queue is full adding user having name = Ram: false adding user having name = Mohan: false
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedExceptionParameter: The method takes three parameters:
// Program Demonstrate offer(E e, long timeout, TimeUnit unit)
// method of ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// Define capacity of ArrayBlockingQueue
int capacity = 5;
// Create object of ArrayBlockingQueue
ArrayBlockingQueue<Integer> queue =
new ArrayBlockingQueue<Integer>(capacity);
// Add 5 elements to ArrayBlockingQueue having
// Timeout in seconds with value 10 secs
System.out.println("adding 423: " +
queue.offer(433, 10, TimeUnit.SECONDS));
System.out.println("adding 456: " +
queue.offer(456, 10, TimeUnit.SECONDS));
System.out.println("adding 987: " +
queue.offer(987, 10, TimeUnit.SECONDS));
System.out.println("adding 578: " +
queue.offer(578, 10, TimeUnit.SECONDS));
System.out.println("adding 852: " +
queue.offer(852, 10, TimeUnit.SECONDS));
// Check whether queue is full or not
if (queue.remainingCapacity() == 0) {
System.out.println("queue is full");
System.out.println("queue contains " + queue);
}
else {
System.out.println("queue is not full");
System.out.println("queue contains " + queue);
}
// Try to add more elements
System.out.println("adding 546: " +
queue.offer(546, 10, TimeUnit.SECONDS));
}
}
Output:
adding 423: true adding 456: true adding 987: true adding 578: true adding 852: true queue is full queue contains [433, 456, 987, 578, 852] adding 546: false
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4