BlockingQueue remainingCapacity() method in Java with examples Last Updated : 15 Oct, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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); } } Output: Queue is [John, Tom, Clark, Kat] Remaining Capacity of Queue is 3 Program 2: Java // 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(); } } Output: 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 Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#remainingCapacity() Comment More infoAdvertise with us Next Article BlockingQueue remainingCapacity() method in Java with examples G gopaldave Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Java-BlockingQueue +1 More Practice Tags : JavaJava-Collections Similar Reads DelayQueue remainingCapacity() method in Java with Examples The remainingCapacity() method of DelayQueue always returns Integer.MAX_VALUE because a DelayQueue is not capacity constrained. That means, irrespective of the size of the DelayQueue it returns same result, i.e. Integer.MAX_VALUE. Syntax: public int remainingCapacity () Return Value: The function re 2 min read BlockingQueue put() method in Java with examples The put(E e) method of BlockingQueue interface inserts element passed as parameter to method at the tail of this BlockingQueue, if queue is not full. If the queue is full, then this method will wait for space to become available and after space is available, it inserts the element to BlockingQueue. 3 min read BlockingQueue remove() method in Java with examples The remove(Object obj) method of BlockingQueue removes only one instance of the given Object, passed as parameter, from this BlockingQueue if it is present. It removes an element e such that obj.equals(e) and if this queue contains one or more instance of element e. This method returns true if this 4 min read 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 3 min read BlockingQueue take() method in Java with examples The take() method of BlockingQueue interface is used to retrieve and remove the head of this queue. If the queue is empty then it will wait until an element becomes available. This method is more efficient if working on threads and using BlockingQueue in that process. So the thread that initially ca 4 min read Like