Open In App

ArrayBlockingQueue clear() Method in Java

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.

  • ArrayBlockingQueue class is a member of the Java Collections Framework.
  • Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
  • The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
  • If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.

The clear() method removes all of the elements from this queue. After applying this method the queue will become empty. Syntax:

public void clear()

Below programs illustrate clear() method of ArrayBlockingQueue. Example 1 

Output :
After adding numbers
[23, 32, 45, 12]
After clear() method
[]

Example 2 

Output :
List of Names: [Aman, Siddhant, Mahafuj, Raunak, Suvo]
Remaining Capacity: 0
List of Names: []
Remaining Capacity: 5

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#clear()


Similar Reads