ArrayBlockingQueue是有界的阻塞隊列,該隊列內部使用數組支持的元素存儲。
- ArrayBlockingQueue類是Java Collections Framework的成員。
- 有界意味著它將具有固定的大小,您不能存儲數量超過隊列容量的元素。
- 隊列還遵循FIFO(先進先出)規則,用於存儲和刪除隊列中的元素。
- 如果您嘗試將一個元素放入一個完整的隊列或從一個空隊列中取出一個元素,那麽該隊列將阻塞。
remainingCapacity()方法返回可以添加到隊列而不會阻塞的更多元素的數量。這始終等於此隊列的初始容量與該隊列的當前大小之差。
用法:
public int remainingCapacity()
參數:該方法不帶任何參數。
返回值:該方法返回隊列的剩餘容量。
以下示例程序旨在說明ArrayBlockingQueue的remainingCapacity()方法:
示例1:
// Program to demonstrate remainingCapacity() method
// of ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// Define capacity of ArrayBlockingQueue
int capacity = 5;
// Create object of ArrayBlockingQueue
ArrayBlockingQueue<Integer> queue = new
ArrayBlockingQueue<Integer>(capacity);
// Add elements to ArrayBlockingQueue
queue.add(23);
queue.add(32);
// Print queue after adding numbers
System.out.println("Queue :" + queue);
// Check remaining capacity
int remainingCapacity = queue.remainingCapacity();
System.out.println("Remaining Capacity:" +
remainingCapacity);
// Add elements to ArrayBlockingQueue
queue.add(54);
queue.add(78);
// Print queue after adding numbers
System.out.println("Queue :" + queue);
// Check remaining capacity
System.out.println("Remaining Capacity:" +
queue.remainingCapacity());
}
}
輸出:
Queue :[23, 32] Remaining Capacity:3 Queue :[23, 32, 54, 78] Remaining Capacity:1
示例2:
// Program to demonstrate remainingCapacity() method
// of ArrayBlockingQueue
import java.util.concurrent.ArrayBlockingQueue;
public class GFG {
// Create a User Object with name and age as an attribute
public class User {
public String name;
public String age;
User(String name, String age)
{
this.name = name;
this.age = age;
}
}
// Main Method
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.RemainingCapacityExample();
}
// Method to give example of contains function
public void RemainingCapacityExample()
{
// Define capacity of ArrayBlockingQueue
int capacity = 5;
// Create object of ArrayBlockingQueue
ArrayBlockingQueue<User> queue = new
ArrayBlockingQueue<User>(capacity);
// Create user objects
User user1 = new User("Aman", "24");
User user2 = new User("Amar", "23");
User user3 = new User("Sanjeet", "25");
// Add Objects to ArrayBlockingQueue
queue.offer(user1);
queue.offer(user2);
queue.offer(user3);
// Check remaining capacity
int remainingCapacity = queue.remainingCapacity();
System.out.println("Remaining Capacity:" +
remainingCapacity);
User user4 = new User("Suvo", "26");
User user5 = new User("Ravi", "22");
// Adding more objects
queue.offer(user2);
queue.offer(user3);
// Check remaining capacity
remainingCapacity = queue.remainingCapacity();
System.out.println("Remaining Capacity:" +
remainingCapacity);
}
}
輸出:
Remaining Capacity:2 Remaining Capacity:0
相關用法
- Java LinkedBlockingQueue remainingCapacity()用法及代碼示例
- Java PriorityBlockingQueue remainingCapacity()用法及代碼示例
- Java LinkedBlockingDeque remainingCapacity()用法及代碼示例
- Java BlockingQueue remainingCapacity()用法及代碼示例
- Java DelayQueue remainingCapacity()用法及代碼示例
- Java LinkedTransferQueue remainingCapacity()用法及代碼示例
- Java ArrayBlockingQueue put()用法及代碼示例
- Java ArrayBlockingQueue take()用法及代碼示例
- Java ArrayBlockingQueue add()用法及代碼示例
- Java ArrayBlockingQueue contains()用法及代碼示例
- Java ArrayBlockingQueue offer()用法及代碼示例
- Java ArrayBlockingQueue spliterator()用法及代碼示例
- Java ArrayBlockingQueue peek()用法及代碼示例
- Java ArrayBlockingQueue drainTo()用法及代碼示例
- Java ArrayBlockingQueue iterator()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayBlockingQueue remainingCapacity() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。