Home  >  Core Java

Example of ConcurrentLinkedQueue in Java

By Arvind Rai, December 09, 2012
1. ConcurrentLinkedQueue is based on linked node. It is named as ConcurrentLinkedQueue because it is thread safe and can be accessed concurrently.
2. As a property of queue, ConcurrentLinkedQueue follows FIFO. Element is added to the tail of the queue and element is pull out from head.
3. ConcurrentLinkedQueue behaves asynchronously.
4. The use of ConcurrentLinkedQueue is that when we need to use a collection in multi-threaded environment.
5. ConcurrentLinkedQueue is available from jdk1.5.
package com.concretepage;

import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;


public class ConcurrentLinkedQueueTest {
	
	ConcurrentLinkedQueue<String> clq= new ConcurrentLinkedQueue<String>();
	
	class AddThread implements Runnable{

		@Override
		public void run() {
			//ads the element to the tail of element
			clq.add("A");
			
			//insert the elements to the tail of element
			clq.offer("B");	
			
			// retrieve and removes the elements from head
			clq.poll();
			
			Iterator<String> itr= clq.iterator();
			while(itr.hasNext()){
				System.out.println(itr.next());
			}
	   }
	}
	
   public static void main(String... args){
	   new Thread(new ConcurrentLinkedQueueTest().new AddThread()).start();
	   new Thread(new ConcurrentLinkedQueueTest().new AddThread()).start();
   }
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us