Java LinkedTransferQueue

In this tutorial, we will learn about java.util.concurrent.LinkedTransferQueue, its features along with the working examples including explanation of those.

LinkedTransferQueue class

In Java, LinkedTransferQueue is a class under the package of concurrent collections and it is an implementation of the BlockingQueue and this is unbounded queue. Unlike the other implementation of BlockingQueue, this class methods will not wait for adding the elements into the queue (or) will not block the thread or will not return false, this means while adding elements into the queue instantaneous.

And this class provides additional methods as compare to the other implementations of BlockingQueue interface, which are transfer and tryTransfer responsible for adding elements into the queue.

LinkedTransferQueue methods.

offer(E e) : Inserts the element into the queue and this will never return false as the queue is unbounded in nature.

transfer(E e) : It will transfer the element to the waiting consumer, if the consumer is already waiting for the element otherwise it will insert the element end of queue and waits until consumer receives that element.

tryTransfer(E e) : It will transfer the element to the waiting consumer, if the consumer is already waiting for the element otherwise it will wait till timed out afterwards will return false and will not enqueue the element into the queue.

LinkedTransferQueue offer(),transfer(),tryTransfer() and tryTransfer() methods example:

In this example, we will explore most important methods of the LinkedTransferQueue.

CopiedCopy Code
import java.util.concurrent.LinkedTransferQueue;
public class LinkedTransferQueueExample extends Thread {
	public LinkedTransferQueue<String> queue;
	LinkedTransferQueueExample(LinkedTransferQueue<String> queue,String name) {
		super(name);
		this.queue =queue; 
	}
	public static void main(String[] args) {
		LinkedTransferQueue<String> queueObject = new LinkedTransferQueue<>();
		LinkedTransferQueueExample consumer = new LinkedTransferQueueExample(queueObject,"consumer"); 
		LinkedTransferQueueExample producer = new LinkedTransferQueueExample(queueObject,"producer"); 
		consumer.start();
		producer.start();
	}
	public void run() {
		try {
		String nameofThread = Thread.currentThread().getName(); 
		if(nameofThread.equals("producer")) {
			System.out.println("E1 transferring:");
			this.queue.transfer("E1");
			System.out.println("E1 transferred:");
			System.out.println("E2 transferred? "+ this.queue.tryTransfer("E2"));
		} else {
			System.out.println("consumer goes for sleep of 1sec");
			Thread.sleep(1000);
			System.out.println("Element received:"+this.queue.poll());
			System.out.println("Element received:"+this.queue.poll());
		}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}

Output :

consumer goes for sleep of 1sec
E1 transferring:
E1 transferred:
Element received:E1
E2 transferred? false
Element received:null

Explanation:

In the above example, we kept consumer goes for a sleep of 1s. So that producer thread will wait until transferred element E1 reached to the consumer because used method transfer method, after 1second consumer as woken up and picked one element 'E1'.

In the second insertion by the consumer not used transfer() method instead of it used tryTransfer() method and it not waits hence given the result as false because consumer was not waiting for it.

In the consumer thread second poll returned null because there were no elements in the queue.

Both poll() and tryTransfer() methods of above example immediately checks whether anything is producing or consuming and if not returned false for producer and null for consumer.

Conclusion:

In this tutorial, we learned about java.util.concurrent.LinkedTransferQueue, its features along with the working examples including explanation of those.