Java ArrayBlockingQueue remainingCapacity() Method
The remainingCapacity() method of ArrayBlockingQueue returns the remaining capacity of the queue. It returns an integer value. For example, if the queue capacity is ten, and the queue contains six elements, then the remainingCapacity() method returns four.
Syntax:
public int remainingCapacity()
Program to show ArrayBlockingQueue remainingCapacity()
Let’s have a look at the program.
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class ArrayBlockingQueueRemainingCapacityExample { public static void main(String[] args) throws InterruptedException { int capacity = 10; BlockingQueuebq = new ArrayBlockingQueue<>(capacity); bq.add(100); bq.add(200); bq.add(300); System.out.println("Queue elements: \n" + bq); int remainingCapacity = bq.remainingCapacity(); System.out.println("remaining capacity of the queue: " + remainingCapacity); bq.add(400); bq.add(500); System.out.println("Queue elements after adding 2 more elements: \n" + bq); remainingCapacity = bq.remainingCapacity(); System.out.println("remaining capacity of the queue: " + remainingCapacity); } }
Output:
Queue elements: [100, 200, 300] remaining capacity of the queue: 7 Queue elements after adding 2 more elements: [100, 200, 300, 400, 500] remaining capacity of the queue: 5
As shown in the above example, the initial capacity of the queue is ten, and then we added three elements; hence remaining capacity becomes seven. Again we inserted two more elements into the queue, so now the remaining capacity is five.
Program 2:
Example with custom Employee objects.
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 RemainingCapacityDemo { public static void main(String[] args) throws InterruptedException { int capacity = 10; BlockingQueueemployees = new ArrayBlockingQueue<>(capacity); employees.put(new Employee(101, "John")); employees.put(new Employee(102, "Adam")); employees.put(new Employee(103, "Ricky")); System.out.println("Queue elements: "); for (Employee employee : employees) { System.out.println(employee); } int remainingCapacity = employees.remainingCapacity(); System.out.println("remaining capacity of the queue: " + remainingCapacity); employees.put(new Employee(104, "Shaun")); employees.put(new Employee(105, "Chris")); System.out.println("Queue elements after adding 2 more elements: "); for (Employee employee : employees) { System.out.println(employee); } remainingCapacity = employees.remainingCapacity(); System.out.println("remaining capacity of the queue: " + remainingCapacity); } }
Output:
Queue elements: Employee [employeeId=101, employeeName=John] Employee [employeeId=102, employeeName=Adam] Employee [employeeId=103, employeeName=Ricky] remaining capacity of the queue: 7 Queue elements after adding 2 more 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] remaining capacity of the queue: 5
Tha’s all about the ArrayBlockingQueue remainingCapacity() method. Please share if you find this article helpful.