Java中的LinkedBlockingQueue的remainingCapacity()方法
LinkedBlockingQueue的 remainingCapacity() 方法返回可以添加到LinkedBlockingQueue中的更多元素而不被阻塞的数量。返回的容量有三种情况:
- 如果剩余容量为零,则无法向LinkedBlockingQueue中添加更多元素。
- 如果LinkedBlockingQueue的剩余容量等于队列的大小,则不能从队列中删除任何元素,因为在这种情况下队列为空。
- 在任何其他情况下,Capacity始终等于此LinkedBlockingQueue的初始容量与当前大小之间的差。
语法:
public int remainingCapacity()
返回值: 此方法返回LinkedBlockingQueue的 剩余容量 。
下面的程序演示了LinkedBlockingQueue类的remainingCapacity()方法:
程序1: 使用remainingCapacity()方法返回LinkedBlockingQueue的剩余容量,其中LinkedBlockingQueue包含名称列表。
// 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");
// using remainingCapacity() method to find remaining Capacity of linkedQueue
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
程序2: 查找包含员工列表的LinkedBlockingQueue的剩余容量。
// Java Program Demonstrate remainingCapacity()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public void findPeek()
{
// 定义LinkedBlockingQueue的容量
int capacityOfQueue = 7;
// 创建LinkedBlockingQueue的对象
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
// 向LinkedBlockingQueue中添加元素
Employee emp1 = new Employee("Ravi", "Tester", "39000");
Employee emp2 = new Employee("Sanjeet", "Manager", "98000");
// 向linkedQueue添加雇员对象
linkedQueue.add(emp1);
linkedQueue.add(emp2);
// 添加元素并查找剩余容量
// 再次按相同的步骤进行
// 直到队列变为满
while (linkedQueue.size() != 7) {
// 一遍又一遍地向队列中添加emp2
System.out.println("Adding employee is success "
+ linkedQueue.offer(emp2));
// 使用remainingCapacity()方法找到linkedQueue的剩余容量
int remain = linkedQueue.remainingCapacity();
// 打印剩余容量的值
System.out.println("Remaining Capacity of list :"
+ remain);
}
}
// 创建一个Employee类,它具有名称、职位和薪水属性
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();
}
}
输出:
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
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#remainingCapacity–
极客教程