Java LinkedTransferQueue

Last modified on November 9th, 2014 by Joe.

This Java tutorial is to learn about the LinkedTransferQueue. It is an implementation of TransferQueue and available in the Java SE API. This class is part of the Java collections framework. LinkedTransferQueue is an unbounded TransferQueue based on linked nodes. A producer will wait for the consumer to consume the elements in this queue.

LinkedTransferQueue was introduced in Java SE7 along with the TransferQueue. This queue is one of the most capable implementation in the concurrent collections. TransferQueue entends the BlockingQueue thus the LinkedTransferQueue has additional methods of the TransferQueue also.

LinkedTransferQueue Example

Following example for LinkedTransferQueue is based on the classic producer consumer use case. Producer waits for the consumer and pushes the element to the LinkedTransferQueue and hence consumed.

LinkedTransferQueueProducer.java

package com.javapapers.java.collections;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TransferQueue;

public class LinkedTransferQueueProducer implements Runnable {

	protected TransferQueue<String> transferQueue;
	final Random random = new Random();

	public LinkedTransferQueueProducer(TransferQueue<String> queue) {
		this.transferQueue = queue;
	}

	@Override
	public void run() {
		while (true) {
			try {
				String data = UUID.randomUUID().toString();
				System.out.println("Put: " + data);
				transferQueue.transfer(data);
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

LinkedTransferQueueConsumer.java

package com.javapapers.java.collections;

import java.util.concurrent.TransferQueue;

public class LinkedTransferQueueConsumer implements Runnable {

	protected TransferQueue<String> transferQueue;

	public LinkedTransferQueueConsumer(TransferQueue<String> queue) {
		this.transferQueue = queue;
	}

	@Override
	public void run() {
		while (true) {
			try {
				String data = transferQueue.take();
				System.out.println(Thread.currentThread().getName()
						+ " take(): " + data);
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

LinkedTransferQueueExample.java

package com.javapapers.java.collections;

import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

public class LinkedTransferQueueExample {
	public static void main(String[] args) {
		final TransferQueue<String> transferQueue = new LinkedTransferQueue<String>();

		LinkedTransferQueueProducer queueProducer = new LinkedTransferQueueProducer(
				transferQueue);
		new Thread(queueProducer).start();

		LinkedTransferQueueConsumer queueConsumer1 = new LinkedTransferQueueConsumer(
				transferQueue);
		new Thread(queueConsumer1).start();

		LinkedTransferQueueConsumer queueConsumer2 = new LinkedTransferQueueConsumer(
				transferQueue);
		new Thread(queueConsumer2).start();

	}
}

Example Output

Put: d9458fb8-ae52-499c-aac6-06f57cf2b4fd
Thread-2 take(): d9458fb8-ae52-499c-aac6-06f57cf2b4fd
Put: e29e0991-5def-46c8-bb09-2cf6fd55afed
Thread-1 take(): e29e0991-5def-46c8-bb09-2cf6fd55afed
Put: 6df9a70f-987c-4298-ba56-4d7fe3fa8e06
Thread-2 take(): 6df9a70f-987c-4298-ba56-4d7fe3fa8e06
Put: a1080f5f-8f33-43fe-905e-6b6c4e7cba1f
Thread-1 take(): a1080f5f-8f33-43fe-905e-6b6c4e7cba1f
Put: cd2af5c3-725c-40b6-b04a-b715187c9c1c
Thread-2 take(): cd2af5c3-725c-40b6-b04a-b715187c9c1c

Comments on "Java LinkedTransferQueue"

  1. […] LinkedTransferQueue […]

  2. […] LinkedTransferQueue – an implementation class of TransferQueue. […]

Comments are closed for "Java LinkedTransferQueue".