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:
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public static void main(String[] args)
{
int capacityOfQueue = 7 ;
BlockingQueue<String> BQ
= new LinkedBlockingQueue<String>(
capacityOfQueue);
BQ.add( "John" );
BQ.add( "Tom" );
BQ.add( "Clark" );
BQ.add( "Kat" );
int remainingCapacity
= BQ.remainingCapacity();
System.out.println( "Queue is " + BQ);
System.out.println( "Remaining Capacity of Queue is "
+ remainingCapacity);
}
}
|
Output:
Queue is [John, Tom, Clark, Kat]
Remaining Capacity of Queue is 3
Program 2:
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class GFG {
public void findPeek()
{
int capacityOfQueue = 7 ;
BlockingQueue<Employee> BQ
= new LinkedBlockingQueue<Employee>(
capacityOfQueue);
Employee emp1
= new Employee( "Ravi" , "Tester" , "39000" );
Employee emp2
= new Employee( "Sanjeet" , "Manager" , "98000" );
BQ.add(emp1);
BQ.add(emp2);
while (BQ.size() != 7 ) {
System.out.println(
"Adding employee is success "
+ BQ.offer(emp2));
int remain = BQ.remainingCapacity();
System.out.println(
"Remaining Capacity of list :"
+ remain);
}
}
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 + "]" ;
}
}
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()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 Oct, 2019
Like Article
Save Article