ArrayBlockingQueue是有界的阻塞隊列,該隊列內部使用數組支持的元素存儲。
- ArrayBlockingQueue類是Java Collections Framework的成員。
- 有界意味著它將具有固定的大小,您不能存儲數量超過隊列容量的元素。
- 隊列還遵循FIFO(先進先出)規則,用於存儲和刪除隊列中的元素。
- 如果您嘗試將一個元素放入一個完整的隊列或從一個空隊列中取出一個元素,那麽該隊列將阻塞。
clear()方法從此隊列中刪除所有元素。應用此方法後,隊列將為空。
用法:
public void clear()
下麵的程序說明ArrayBlockingQueue的clear()方法。
例子1
// Java Program Demonstrate clear()
// 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 element to ArrayBlockingQueue
queue.add(23);
queue.add(32);
queue.add(45);
queue.add(12);
// print queue after adding numbers
System.out.println("After addding numbers");
System.out.println(queue);
// Apply clear() method
queue.clear();
// print queue after clear() operation
System.out.println("After clear() method");
System.out.println(queue);
}
}
Output : After addding numbers [23, 32, 45, 12] After clear() method []
例子2
// Java Program Demonstrate clear()
// 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 to store 5 names
ArrayBlockingQueue<String> names
= new ArrayBlockingQueue<String>(capacity);
// Add element to ArrayBlockingQueue
names.add("Aman");
names.add("Siddhant");
names.add("Mahafuj");
names.add("Raunak");
names.add("Suvo");
// print queue after adding numbers
System.out.println("List of Names: " + names);
System.out.println("Remaining Capacity: " + names.remainingCapacity());
// Apply clear() method
names.clear();
// print queue after clear() operation
System.out.println("List of Names: " + names);
System.out.println("Remaining Capacity: " + names.remainingCapacity());
}
}
Output : List of Names: [Aman, Siddhant, Mahafuj, Raunak, Suvo] Remaining Capacity: 0 List of Names: [] Remaining Capacity: 5
參考:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#clear()
相關用法
- Java ArrayBlockingQueue put()用法及代碼示例
- Java ArrayBlockingQueue add()用法及代碼示例
- Java ArrayBlockingQueue take()用法及代碼示例
- Java ArrayBlockingQueue contains()用法及代碼示例
- Java ArrayBlockingQueue toString()用法及代碼示例
- Java ArrayBlockingQueue size()用法及代碼示例
- Java ArrayBlockingQueue poll()用法及代碼示例
- Java ArrayBlockingQueue peek()用法及代碼示例
- Java ArrayBlockingQueue remainingCapacity()用法及代碼示例
- Java ArrayBlockingQueue offer()用法及代碼示例
- Java ArrayBlockingQueue spliterator()用法及代碼示例
- Java ArrayBlockingQueue iterator()用法及代碼示例
- Java ArrayBlockingQueue drainTo()用法及代碼示例
- Java ArrayBlockingQueue toArray()用法及代碼示例
- Java ArrayBlockingQueue remove()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayBlockingQueue clear() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。