The remainingCapacity() method of BlockingQueue returns the number of more elements that can be added to BlockingQueue without blocking.
The Capacity returned arises in three cases:
- If remaining Capacity is Zero, then no more elements can be added to the BlockingQueue.
- If remaining Capacity of BlockingQueue is equal to the size of the Queue, then no element can be removed from the queue because in such situation queue is empty.
- In any other case, Capacity is always equal to a difference between the initial capacity of this BlockingQueue and the current size of this BlockingQueue.
Syntax:
public int remainingCapacity()
Return Value: This method returns the remaining capacity of the BlockingQueue.
Note: The remainingCapacity() method of BlockingQueue has been inherited from the Queue class in Java.
Below programs illustrates remainingCapacity() method of BlockingQueue class:
Program 1:
// Java Program Demonstrate remainingCapacity() // method of BlockingQueue import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; public class GFG { public static void main(String[] args) { // define capacity of BlockingQueue int capacityOfQueue = 7; // create object of BlockingQueue BlockingQueue<String> BQ = new LinkedBlockingQueue<String>( capacityOfQueue); // Add element to BlockingQueue BQ.add("John"); BQ.add("Tom"); BQ.add("Clark"); BQ.add("Kat"); // find remaining Capacity of BQ // using remainingCapacity() method int remainingCapacity = BQ.remainingCapacity(); // print result System.out.println("Queue is " + BQ); // print head of the queue System.out.println("Remaining Capacity of Queue is " + remainingCapacity); } } |
Queue is [John, Tom, Clark, Kat] Remaining Capacity of Queue is 3
Program 2:
// Java Program Demonstrate remainingCapacity() // method of BlockingQueue import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.BlockingQueue; public class GFG { public void findPeek() { // define capacity of BlockingQueue int capacityOfQueue = 7; // create object of BlockingQueue BlockingQueue<Employee> BQ = new LinkedBlockingQueue<Employee>( capacityOfQueue); // Add element to BlockingQueue Employee emp1 = new Employee("Ravi", "Tester", "39000"); Employee emp2 = new Employee("Sanjeet", "Manager", "98000"); // Add Employee Objects to BQ BQ.add(emp1); BQ.add(emp2); // add element and find remaining capacity // follow same process again // until the queue becomes full while (BQ.size() != 7) { // adding emp2 again and again to queue System.out.println( "Adding employee is success " + BQ.offer(emp2)); // find remaining capacity of BQ // using remainingCapacity() method int remain = BQ.remainingCapacity(); // print remaining capacity value System.out.println( "Remaining Capacity of list :" + remain); } } // create an Employee Object with name, // position and salary as an attribute public class Employee { public String name; public String position; public String salary; Employee(String name, String position, String salary) { this.name = name; this.position = position; this.salary = salary; } @Override public String toString() { return "Employee [name=" + name + ", position=" + position + ", salary=" + salary + "]"; } } // Main Method public static void main(String[] args) { GFG gfg = new GFG(); gfg.findPeek(); } } |
Adding employee is success true Remaining Capacity of list :4 Adding employee is success true Remaining Capacity of list :3 Adding employee is success true Remaining Capacity of list :2 Adding employee is success true Remaining Capacity of list :1 Adding employee is success true Remaining Capacity of list :0
Attention reader! Donât stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- LinkedTransferQueue remainingCapacity() method in Java with Examples
- DelayQueue remainingCapacity() method in Java with Examples
- BlockingQueue drainTo() method in Java with examples
- BlockingQueue take() method in Java with examples
- BlockingQueue remove() method in Java with examples
- BlockingQueue contains() method in Java with examples
- BlockingQueue put() method in Java with examples
- BlockingQueue offer() method in Java with examples
- BlockingQueue poll() method in Java with examples
- ArrayBlockingQueue remainingCapacity() Method in Java
- LinkedBlockingQueue remainingCapacity() method in Java
- PriorityBlockingQueue remainingCapacity() method in Java
- LinkedBlockingDeque remainingCapacity() method in Java
- BlockingQueue add() in Java with examples
- BlockingQueue Interface in Java
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
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.

