The remainingCapacity() method of LinkedBlockingQueue returns the number of more elements that can be added to LinkedBlockingQueue without blocking. The Capacity returned arises three cases:
- If remaining Capacity is Zero, then no more elements can be added to the LinkedBlockingQueue.
- If remaining Capacity of LinkedBlockingQueue 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 LinkedBlockingQueue and the current size of this LinkedBlockingQueue.
Syntax:
public int remainingCapacity()
Return Value: This method returns the remaining capacity of the LinkedBlockingQueue.Â
Below programs illustrates remainingCapacity() method of LinkedBlockingQueue class:Â
Program 1: Returning the remaining Capacity of the LinkedBlockingQueue using remainingCapacity() method where LinkedBlockingQueue contains list of names.Â
Java
// Java Program Demonstrate remainingCapacity() // method of LinkedBlockingQueue Â
import java.util.concurrent.LinkedBlockingQueue; public class GFG { Â
    public static void main(String[] args)     {         // define capacity of LinkedBlockingQueue         int capacityOfQueue = 7 ; Â
        // create object of LinkedBlockingQueue         LinkedBlockingQueue<String> linkedQueue             = new LinkedBlockingQueue<String>(capacityOfQueue); Â
        // Add element to LinkedBlockingQueue         linkedQueue.add("John");         linkedQueue.add("Tom");         linkedQueue.add("Clark");         linkedQueue.add("Kat"); Â
        // find remaining Capacity of linkedQueue         // using remainingCapacity() method         int remainingCapacity = linkedQueue.remainingCapacity(); Â
        // print result         System.out.println("Queue is " + linkedQueue); Â
        // print head of queue         System.out.println("Remaining Capacity of Queue is "                            + remainingCapacity);     } } |
Queue is [John, Tom, Clark, Kat] Remaining Capacity of Queue is 3
Program 2: Finding Remaining Capacity of LinkedBlockingQueue which contains list of Employees.Â
Java
// Java Program Demonstrate remainingCapacity() // method of LinkedBlockingQueue Â
import java.util.concurrent.LinkedBlockingQueue; public class GFG { Â
    public void findPeek()     {         // define capacity of LinkedBlockingQueue         int capacityOfQueue = 7 ; Â
        // create object of LinkedBlockingQueue         LinkedBlockingQueue<Employee> linkedQueue             = new LinkedBlockingQueue<Employee>(capacityOfQueue); Â
        // Add element to LinkedBlockingQueue         Employee emp1 = new Employee("Ravi", "Tester", " 39000 ");         Employee emp2 = new Employee("Sanjeet", "Manager", " 98000 "); Â
        // Add Employee Objects to linkedQueue         linkedQueue.add(emp1);         linkedQueue.add(emp2); Â
        // add element and find remaining capacity         // follow same process again         // until the queue becomes full Â
        while (linkedQueue.size() != 7 ) { Â
            // adding emp2 again and again to queue             System.out.println("Adding employee is success "                                + linkedQueue.offer(emp2)); Â
            // find remaining capacity of linkedQueue             // using remainingCapacity() method             int remain = linkedQueue.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