Java LinkedBlockingDeque

Last modified on November 30th, 2014 by Joe.

This Java tutorial is to learn about the concurrent collection LinkedBlockingDeque. It is an optionally bounded blocking double ended queue. LinkedBlockingDeque is an implementation of the interface Java BlockingDeque.

LinkedBlockingDeque provides a constructor which can be used to specify the capacity and limit the number of elements that can be added to the queue.

LinkedBlockingDeque Example

Following example for LinkedBlockingDeque is based on a producer consumer scenario.

LinkedBlockingDequeProducer.java

package com.javapapers.java.collections;

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

public class LinkedBlockingDequeProducer implements Runnable {
    protected BlockingDeque<String> blockingDeque;
    final Random random = new Random();

    public LinkedBlockingDequeProducer(BlockingDeque<String> queue) {
        this.blockingDeque = queue;
    }

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

LinkedBlockingDequeConsumer.java

package com.javapapers.java.collections;

import java.util.concurrent.BlockingDeque;

public class LinkedBlockingDequeConsumer implements Runnable {

    protected BlockingDeque<String> blockingDeque;

    public LinkedBlockingDequeConsumer(BlockingDeque<String> queue) {
        this.blockingDeque = queue;
    }

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

}

LinkedBlockingDequeExample.java

package com.javapapers.java.collections;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class LinkedBlockingDequeExample {
    public static void main(String[] args) {
        final BlockingDeque<String> priorityBlockingQueue = new LinkedBlockingDeque<String>();

        LinkedBlockingDequeProducer queueProducer = new LinkedBlockingDequeProducer(
                priorityBlockingQueue);
        new Thread(queueProducer).start();

        LinkedBlockingDequeConsumer queueConsumer1 = new LinkedBlockingDequeConsumer(
                priorityBlockingQueue);
        new Thread(queueConsumer1).start();

        LinkedBlockingDequeConsumer queueConsumer2 = new LinkedBlockingDequeConsumer(
                priorityBlockingQueue);
        new Thread(queueConsumer2).start();
    }
}

LinkedBlockingDeque Example Output

Put: e4be54ce-096c-45ae-8af0-f4dd02520030
Thread-2 take(): e4be54ce-096c-45ae-8af0-f4dd02520030
Put: 4e82d8c5-acdd-4e4b-bc55-c5be05c9f665
Thread-1 take(): 4e82d8c5-acdd-4e4b-bc55-c5be05c9f665
Put: 9c983736-4376-4aea-b2d3-2e89f1895c37
Thread-2 take(): 9c983736-4376-4aea-b2d3-2e89f1895c37
Put: 1056baea-d7fa-48bb-99a7-899d17f5bf5f
Thread-1 take(): 1056baea-d7fa-48bb-99a7-899d17f5bf5f
Put: 89389158-5d9b-427f-a39c-b870f3d5cde7
Thread-2 take(): 89389158-5d9b-427f-a39c-b870f3d5cde7
Put: ea042179-4193-4716-86f3-dad9b6c2b8cb
Thread-1 take(): ea042179-4193-4716-86f3-dad9b6c2b8cb
Put: 48692ecc-2cbe-4b6d-950c-01ee7f0d4d15
Thread-2 take(): 48692ecc-2cbe-4b6d-950c-01ee7f0d4d15
Put: da78bb0a-9b4b-4256-b2f8-db7c3c0476b1

Comments on "Java LinkedBlockingDeque"

  1. […] LinkedBlockingDeque […]

Comments are closed for "Java LinkedBlockingDeque".