LinkedBlockingQueue的clear()方法从此队列中删除所有元素。应用此方法后,队列将为空。
用法:
public void clear()
以下示例程序旨在说明LinkedBlockingQueue类的clear()方法:
示例1:
// Java Program Demonstrate clear()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 50;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<Integer> linkedQueue
= new LinkedBlockingQueue<Integer>(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add(2300);
linkedQueue.add(1322);
linkedQueue.add(8945);
linkedQueue.add(6512);
// print queue
System.out.println("LinkedBlockingQueue before using"
+ " clear() : " + linkedQueue);
// Apply clear() method
linkedQueue.clear();
// print queue
System.out.println();
System.out.println("LinkedBlockingQueue after using"
+ " clear() method : " + linkedQueue);
}
}
输出:
LinkedBlockingQueue before using clear() : [2300, 1322, 8945, 6512] LinkedBlockingQueue after using clear() method : []
示例2:
// Java Program Demonstrate clear()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 50;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> names
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to ArrayBlockingQueue
names.add("Shubham");
names.add("Siddhant");
names.add("Mahafuj");
names.add("Arka");
names.add("Raunak");
// print queue
System.out.println("LinkedBlockingQueue before using"
+ " clear() : " + names);
// Apply clear() method
names.clear();
// print queue
System.out.println();
System.out.println("LinkedBlockingQueue after using"
+ " clear() method : " + names);
}
}
输出:
LinkedBlockingQueue before using clear() : [Shubham, Siddhant, Mahafuj, Arka, Raunak] LinkedBlockingQueue after using clear() method : []
参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#clear–
相关用法
- Java LinkedBlockingQueue contains()用法及代码示例
- Java LinkedBlockingQueue drainTo()用法及代码示例
- Java LinkedBlockingQueue remainingCapacity()用法及代码示例
- Java LinkedBlockingQueue peek()用法及代码示例
- Java LinkedBlockingQueue put()用法及代码示例
- Java LinkedBlockingQueue take()用法及代码示例
- Java LinkedBlockingQueue size()用法及代码示例
- Java LinkedBlockingQueue remove()用法及代码示例
- Java LinkedBlockingQueue toArray()用法及代码示例
- Java LinkedBlockingQueue poll()用法及代码示例
- Java LinkedBlockingQueue iterator()用法及代码示例
- Java LinkedBlockingQueue toString()用法及代码示例
- Java 8 LinkedBlockingQueue spliterator()用法及代码示例
- Java LinkedBlockingQueue toString()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LinkedBlockingQueue clear() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。