Java Collections Framework - ConcurrentLinkedQueue class

Java Collections Framework - ConcurrentLinkedQueue class


Posted in : Core Java Posted on : March 29, 2011 at 6:20 PM Comments : [ 0 ]

ConcurrentLinkedQueue class is a boundless thread-safe queue which is based on linked nodes. The elements of this queue are ordered in FIFO (first-in-first-out).

Java Collections Framework - ConcurrentLinkedQueue class

ConcurrentLinkedQueue class is a boundless thread-safe queue which is based on linked nodes. The elements of this queue are ordered in FIFO (first-in-first-out). In this queue the element that has been the longest time on the queue is the head element, and the element that has been the shortest time on the queue is the tail element of the queue. Insertion of an element in this queue is happened at tail and the element is retrieved from the head position. ConcurrentLinkedQueue do not allows the 'null' elements. When there are multiple threads accessing a common collection, selection of this queue will be good. All the optional methods of Collection and Iterator interfaces are implemented by this class and its iterator.

Syntax

public class ConcurrentLinkedQueue<E>

Parameter description

E : It is the element's type that's held by this queue.

Constructor of ConcurrentLinkedQueue :

  • ConcurrentLinkedQueue() : Initially an empty ConcurrentLinkedQueue is made by this constructor.
  • ConcurrentLinkedQueue(Collection<? extends E> c) : This constructor makes a ConcurrentLinkedQueue that contains the elements of a particular collection at the time of creation, these elements are arranged in queue as their traversing order of the collection's iterator.

Example :

Here we are making a simple example which will illustrate you that how can ConcurrentLinkedQueue class be implemented.

package devmanuals.com;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ConcurrentLinkedQueueDemo {
  public static void main(String args[]){
    ConcurrentLinkedQueue clq = new ConcurrentLinkedQueue();
    clq.add("A");
    clq.add("B");
    clq.add("C");
    clq.add("D");
    System.out.println("Element of queue = "+clq);
    //peek() method
    Object obj = clq.peek();
    System.out.println("Head element of queue = "+obj);
    //isEmpty() method 
    boolean bol =clq.isEmpty();
    System.out.println("Queue is empty : "+bol);
    //contains() method 
    boolean bol1 =clq.contains("D");
    System.out.println("Is element 'D' existed into queue ? "+bol1);
    //poll() method
    Object obj1 = clq.poll();
    System.out.println("Removed head element = "+obj1);
    //size() method
    int i = clq.size();
    System.out.println("Size of queue = "+i);
  }
}

Output :

Element of queue = [A, B, C, D]

Head element of queue = A

Queue is empty : false

Is element 'D' existed into queue ? true

Removed head element = A

Size of queue = 3

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics