ArrayBlockingQueue remainingCapacity() Method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.
- ArrayBlockingQueue class is a member of the Java Collections Framework.
- Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
- The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
- If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.
The remainingCapacity() method returns number of more elements that can be added to queue without blocking. This is always equal to difference between the initial capacity of this queue and the current size of this queue.
Syntax:
public int remainingCapacity()
Parameters: The method does not take any parameters.
Return Value: The method returns the remaining capacity of the queue.
Below programs illustrate remainingCapacity() method of ArrayBlockingQueue:
Program 1:
// Program to demonstrate remainingCapacity() 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 elements to ArrayBlockingQueue queue.add(23); queue.add(32); // Print queue after adding numbers System.out.println("Queue :" + queue); // Check remaining capacity int remainingCapacity = queue.remainingCapacity(); System.out.println("Remaining Capacity:" + remainingCapacity); // Add elements to ArrayBlockingQueue queue.add(54); queue.add(78); // Print queue after adding numbers System.out.println("Queue :" + queue); // Check remaining capacity System.out.println("Remaining Capacity:" + queue.remainingCapacity()); } } |
Queue :[23, 32] Remaining Capacity:3 Queue :[23, 32, 54, 78] Remaining Capacity:1
Program 2:
// Program to demonstrate remainingCapacity() 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.RemainingCapacityExample(); } // Method to give example of contains function public void RemainingCapacityExample() { // Define 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"); // Add Objects to ArrayBlockingQueue queue.offer(user1); queue.offer(user2); queue.offer(user3); // Check remaining capacity int remainingCapacity = queue.remainingCapacity(); System.out.println("Remaining Capacity:" + remainingCapacity); User user4 = new User("Suvo", "26"); User user5 = new User("Ravi", "22"); // Adding more objects queue.offer(user2); queue.offer(user3); // Check remaining capacity remainingCapacity = queue.remainingCapacity(); System.out.println("Remaining Capacity:" + remainingCapacity); } } |
Remaining Capacity:2 Remaining Capacity:0
Recommended Posts:
- LinkedBlockingDeque remainingCapacity() method in Java
- LinkedBlockingQueue remainingCapacity() method in Java
- PriorityBlockingQueue remainingCapacity() method in Java
- DelayQueue remainingCapacity() method in Java with Examples
- LinkedTransferQueue remainingCapacity() method in Java with Examples
- ArrayBlockingQueue put() method in Java
- ArrayBlockingQueue contains() method in Java
- ArrayBlockingQueue add() method in Java
- ArrayBlockingQueue take() method in Java
- ArrayBlockingQueue offer() Method in Java
- ArrayBlockingQueue spliterator() method in Java
- ArrayBlockingQueue drainTo() Method in Java
- ArrayBlockingQueue iterator() Method in Java
- ArrayBlockingQueue toArray() Method in Java
- ArrayBlockingQueue remove() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



