Java Collection Framework - LinkedBlockingDeque

Java Collection Framework - LinkedBlockingDeque


Posted in : Core Java Posted on : May 18, 2011 at 5:51 PM Comments : [ 0 ]

In Java LinkedBlockingDeque class is a part of Java collection framework and is available in java.util package. This class extends the AbstractQueue class and implements the BlockingDeque interface. This class is a linked nodes based an optional bounded blocking deque of which the unspecified capacity is Integer.MAX_VALUE. We can specify the capacity of this deque as maximum as per requirement.

Java Collection Framework - LinkedBlockingDeque

In Java LinkedBlockingDeque class is a part of Java collection framework and is available in java.util package. This class extends the AbstractQueue class and implements the BlockingDeque interface. This class is linked nodes based an optional bounded blocking deque of which the unspecified capacity is Integer.MAX_VALUE. We can specify the capacity of this deque as maximum as per requirement.

Syntax

public class LinkedBlockingDeque<E> extends AbstractQueue<E>implements BlockingDeque<E>

Constructor of LinkedBlockingDeque

LinkedBlockingDeque() : This constructor makes a LinkedBlockingDeque with default size (Integer.MAX_VALUE).

LinkedBlockingDeque(Collection c) : This constructor makes a LinkedBlockingDeque of size Integer.MAX_VALUE that initially contained the elements of the specified collection.

LinkedBlockingDeque(int capacity) : This constructor makes a LinkedBlockingDeque with the specified capacity.

Methods of LinkedBlockingDeque

LinkedBlockingDeque provides various methods some of them are :

  1. add()
  2.                           syntax : public boolean add(E e)

  3. clear()
  4.                           syntax : public void clear()

  5. getFirst()
  6.                           syntax : public E getFirst()

  7. peek()
  8.                           syntax : public E peek()

  9. poll()
  10.                           syntax : public E poll()

  11. remove()
  12.                           syntax : public E remove()

  13. size()
  14.                           syntax : public int size()

Example :

package devmanuals.com;

import java.util.concurrent.LinkedBlockingDeque;

class LinkedBlockingDequeDemo implements Runnable {

  LinkedBlockingDeque lbd = new LinkedBlockingDeque(1);

  boolean bol = false;

  public void run() {

    try {

      if (bol) {

        Thread.sleep(1000);

        System.out.println("Deleted " + lbd.peek());

        lbd.poll();

      else {

        bol = true;

        System.out.println("Wait for deleting " + lbd.peek());

        lbd.put("B");

        System.out.println("Inserted " + lbd.peek());

      }

    catch (Exception e) {

      e.printStackTrace();

    }

  }

  public static void main(String[] argsthrows Exception {

    LinkedBlockingDequeDemo obj = new LinkedBlockingDequeDemo();

    obj.lbd.offer("A");

    System.out.println("Inserted " + obj.lbd.peek());

    new Thread(obj).start();

    new Thread(obj).start();

  }

}

Output :

Inserted A

Wait for deleting A

Deleted A

Inserted B

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics