BlockingQueue的remainingCapacity()方法返回可以添加到BlockingQueue而不会阻塞的更多元素的数量。
返回的容量在以下三种情况下出现:
- 如果剩余容量为零,则不能将更多元素添加到BlockingQueue。
- 如果BlockingQueue的剩余Capacity等于Queue的大小,则无法从队列中删除任何元素,因为在这种情况下,队列为空。
- 在任何其他情况下,Capacity始终等于此BlockingQueue的初始容量与此BlockingQueue的当前大小之间的差。
用法:
public int remainingCapacity()
返回值:此方法返回BlockingQueue的剩余容量。
注意:BlockingQueue的remainingCapacity()方法已从Java中的Queue类继承。
以下示例程序旨在说明BlockingQueue类的remainingCapacity()方法:
示例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
示例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
相关用法
- Java BlockingQueue take()用法及代码示例
- Java BlockingQueue put()用法及代码示例
- Java BlockingQueue contains()用法及代码示例
- Java DelayQueue remainingCapacity()用法及代码示例
- Java LinkedTransferQueue remainingCapacity()用法及代码示例
- Java BlockingQueue poll()用法及代码示例
- Java BlockingQueue drainTo()用法及代码示例
- Java BlockingQueue offer()用法及代码示例
- Java BlockingQueue remove()用法及代码示例
- Java BlockingQueue add()用法及代码示例
- Java LinkedBlockingDeque remainingCapacity()用法及代码示例
- Java ArrayBlockingQueue remainingCapacity()用法及代码示例
- Java LinkedBlockingQueue remainingCapacity()用法及代码示例
- Java PriorityBlockingQueue remainingCapacity()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 BlockingQueue remainingCapacity() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。