The ConcurrentLinkedQueue
is a thread-safe unbounded queue. The elements are ordered by FIFO (First-In-First-Out). The elements are inserted at the tail (end) and retrieved from the head (start) of the queue. The null
elements are not allowed as an element of the queue. We can use the ConcurrentLinkedQueue
when multiple threads are sharing a single Queue.
The isEmpty
method can be used to check if the ConcurrentLinkedQueue
object has no elements.
public boolean isEmpty()
This method does not take any arguments.
This method returns true
if the queue
object has no elements. Otherwise, it returns false
.
The code below demonstrates how to use the isEmpty
method.
import java.util.concurrent.ConcurrentLinkedQueue;class IsEmpty{public static void main( String args[] ) {ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();System.out.println("The queue is : " + queue);System.out.println("Check if the queue is empty : " + queue.isEmpty());queue.add("1");System.out.println("\nThe queue is : " + queue);System.out.println("Check if the queue is empty : " + queue.isEmpty());}}
In the code above:
In line 1, we import the ConcurrentLinkedQueue
class.
In line 4, we create a ConcurrentLinkedQueue
object with the name queue
. This object doesn’t have any elements in it.
In line 6, we call the isEmpty
method on the queue
object. This method will return true
because the queue
object is empty.
In line 8, we use the add()
method of the queue
object to add one element ("1"
) to queue
.
In line 10, we call the isEmpty
method on the queue
object. This method will return false
because the queue
object contains one element.