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 :
- add()
- clear()
- getFirst()
- peek()
- poll()
- remove()
- size()
syntax : public boolean add(E e)
syntax : public void clear()
syntax : public E getFirst()
syntax : public E peek()
syntax : public E poll()
syntax : public E remove()
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[] args) throws 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 |
[ 0 ] Comments