Java 中 ArrayBlockingQueue clear()方法

原文:https://www . geeksforgeeks . org/arrayblockingqueue-clear-method-in-Java/

ArrayBlockingQueue是有界的,阻塞队列存储由数组支持的内部元素。

  • ArrayBlockingQueue类是 Java Collections 框架的成员。
  • 有界意味着它会有一个固定的大小,你可以不存储的元素数量超过队列的容量。
  • 队列还遵循先进先出规则来存储和移除队列中的元素。
  • 如果你试图把一个元素放到一个满的队列中,或者从一个空的队列中取出一个元素,那么这个队列就会阻塞你。

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()