Home  >  Java 7

TransferQueue and LinkedTransferQueue Java Example

By Arvind Rai, December 30, 2013
java.util.concurrent.TransferQueue has been introduced in JDK 7 and is the part of java collection framework. TransferQueue extends java.util.concurrent.BlockingQueue. The basic difference between TransferQueue and BlockingQueue is that TransferQueue waits for consumer to consume the element. TransferQueue is useful in scenario where message passing need to be guaranteed. TransferQueue makes it possible because producer will wait for consumer to transfer the message. The main method of TransferQueue is transfer(E e) .

transfer(E e) method in TransferQueue

Suppose a producer reaches to TransferQueue to transfer an element and there is consumers waiting to take element, then producer directly transfers the element to consumer. If there is no consumer waiting, then producer will not directly put the element and returned, but it will wait for any consumer to consume the element.

LinkedTransferQueue Java Example

java.util.concurrent.LinkedTransferQueue has been introduced in JDK 7 and is the part of java collection framework. LinkedTransferQueue is unbounded TransferQueue. It means LinkedTransferQueue has no size restriction. This queue follows first-in-first-out (FIFO) concept to get and add element. We will discuss some methods of LinkedTransferQueue.

transfer(E e) : Inherited from TransferQueue in which producer transfers the element to consumer and waits if necessary.
put(E e) : producers put the element and it does not throw exception because there is no size restriction. LinkedTransferQueue is unbounded.
take() : It retrieves the element from queue and wait if empty.

Now find the example of LinkedTransferQueue.
LinkedTransferQueueDemo.java
package com.concretepage.util.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedTransferQueue;
public class LinkedTransferQueueDemo {
   static LinkedTransferQueue<String> lnkTransQueue = new LinkedTransferQueue<String>();
   public static void main(String[] args) {
	    ExecutorService exService = Executors.newFixedThreadPool(2);
	    Producer producer = new LinkedTransferQueueDemo().new Producer();
	    Consumer consumer = new LinkedTransferQueueDemo().new Consumer();
		exService.execute(producer);
		exService.execute(consumer);
		exService.shutdown();
   }
   class Producer implements Runnable{
		@Override
		public void run() {
			for(int i=0;i<3;i++){
				try {
					System.out.println("Producer is waiting to transfer...");
					lnkTransQueue.transfer("A"+i);
					System.out.println("producer transfered element: A"+i);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}	
   }
   class Consumer implements Runnable{
		@Override
		public void run() {
			for(int i=0;i<3;i++){
				try {
					System.out.println("Consumer is waiting to take element...");
					String s= lnkTransQueue.take();
		     		        System.out.println("Consumer received Element: "+s);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
  }
}
 
In the example, there are two runnable threads, one is producer and another is consumer. Producer is transferring the element to consumer and waits if there is no consumer to receive the element. Find the output.
Producer is waiting to transfer...
Consumer is waiting to take element...
Consumer received Element: A0
Consumer is waiting to take element...
producer transfered element: A0
Producer is waiting to transfer...
producer transfered element: A1
Producer is waiting to transfer...
Consumer received Element: A1
Consumer is waiting to take element...
Consumer received Element: A2
producer transfered element: A2
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us