LinkedBlockingQueue的remainingCapacity()方法返回可以無限製添加到LinkedBlockingQueue的更多元素的數量。
返回的Thr容量出現三種情況:
- 如果剩餘容量為零,則不能再將其他元素添加到LinkedBlockingQueue。
- 如果LinkedBlockingQueue的剩餘Capacity等於Queue的大小,則無法從隊列中刪除任何元素,因為在這種情況下隊列為空。
- 在任何其他情況下,Capacity始終等於此LinkedBlockinQueue的初始容量和此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");
// 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
示例2:查找包含員工列表的LinkedBlockingQueue的剩餘容量。
// Java Program Demonstrate peek()
// 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
相關用法
- Java ArrayBlockingQueue remainingCapacity()用法及代碼示例
- Java LinkedBlockingDeque remainingCapacity()用法及代碼示例
- Java PriorityBlockingQueue remainingCapacity()用法及代碼示例
- Java LinkedTransferQueue remainingCapacity()用法及代碼示例
- Java DelayQueue remainingCapacity()用法及代碼示例
- Java BlockingQueue remainingCapacity()用法及代碼示例
- Java LinkedBlockingQueue contains()用法及代碼示例
- Java LinkedBlockingQueue take()用法及代碼示例
- Java LinkedBlockingQueue iterator()用法及代碼示例
- Java LinkedBlockingQueue remove()用法及代碼示例
- Java LinkedBlockingQueue size()用法及代碼示例
- Java LinkedBlockingQueue put()用法及代碼示例
- Java LinkedBlockingQueue toArray()用法及代碼示例
- Java LinkedBlockingQueue peek()用法及代碼示例
- Java LinkedBlockingQueue drainTo()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LinkedBlockingQueue remainingCapacity() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。