The
ConcurrentLinkedQueue
is a thread-safe unbounded queue. It is ordered by. The elements are inserted at the tail (end) and retrieved from the head (start) of the queue. The FIFO First-In-First-Out null
elements are not allowed as an element of the queue. We can use theConcurrentLinkedQueue
when multiple threads are sharing a single Queue.
The ConcurrentLinkedQueue.clear
method can be used to delete all the elements of a ConcurrentLinkedQueue
object.
public void clear()
This method doesn’t take any arguments.
This method is of the void
type, so it doesn’t return any value.
The code below demonstrates how to use the clear
method.
import java.util.concurrent.ConcurrentLinkedQueue;class Clear {public static void main( String args[] ) {ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();queue.add("1");queue.add("2");queue.add("3");System.out.println("The queue is " + queue);queue.clear();System.out.println("After calling the clear method the deque is " + queue);}}
In the above code:
In line 1, we imported the ConcurrentLinkedQueue
from the java.util.concurrent
package.
In line 4, we created an object for the ConcurrentLinkedQueue
class with the name queue
.
In line 5-7, we added three elements to the queue
object using the ConcurrentLinkedQueue.add()
method.
In line 10, we called the clear
method of the queue
object. This will remove all the elements from the queue
and make the queue
empty.