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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。