Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Program to Implement LinkedBlockingDeque API
Next article icon

Java Program to Implement LinkedBlockingDeque API

Last Updated : 13 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The LinkedBlockingDeque class introduced in JDK 1.6 is present in java.util.concurrent package. It is a deque which means a doubly ended queue. The default size of  LinkedBlockingDeque is Integer.MAX_VALUE. It implements the BlockingDeque class and provides an optionally-bounded functionality based on linked nodes. This optional boundedness is served by passing the required size in the constructor and helps in preventing memory wastage. This class blocks a thread if that thread tries to remove elements out of an empty deque.

It implements Serializable, Iterable<E>, Collection<E>, BlockingDeque<E>, BlockingQueue<E>, Deque<E>, Queue<E> interfaces and extends AbstractQueue<E> and AbstractCollection<E> classes.

Declaration:  

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

Here, E is the type of elements stored in the collection.

Implementation of LinkedBlockingDeque API :

Java
// Java program to show the implementation // of LinkedBlockingDeque API  import java.util.*; import java.util.Collection; import java.util.Iterator; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit;  class LinkedBlockingDequeImpl<E> {     private LinkedBlockingDeque<E> linkedBlockingDeque;      // constructor to create a LinkedBlockingDeque .     public LinkedBlockingDequeImpl()     {         linkedBlockingDeque = new LinkedBlockingDeque<E>();     }      // constructor to create a LinkedBlockingDeque initially     // containing the elements of the given collection     public LinkedBlockingDequeImpl(         Collection<? extends E> collection)     {         linkedBlockingDeque             = new LinkedBlockingDeque<E>(collection);     }      // constructor to create a LinkedBlockingDeque with     // specified capacity.     public LinkedBlockingDequeImpl(int cap)     {         linkedBlockingDeque             = new LinkedBlockingDeque<E>(cap);     }      // method to remove all of the elements from the deque.     public void clear() { linkedBlockingDeque.clear(); }      // method to check if deque contains the specified     // element.     public boolean contains(Object object)     {         return linkedBlockingDeque.contains(object);     }      // method to remove all of the elements from the deque     // and add them to specified collection.     public int drainTo(Collection<? super E> collection)     {         return linkedBlockingDeque.drainTo(collection);     }      // method to remove specified number of the elements     // from the deque and add them to specified collection.     public int drainTo(Collection<? super E> collection,                        int maxElement)     {         return linkedBlockingDeque.drainTo(collection,                                            maxElement);     }      // method to return an iterator over the elements in     // this deque .     public Iterator<E> iterator()     {         return linkedBlockingDeque.iterator();     }      // method to insert a specified element at the tail of     // this queue. Return true upon successful insertion     // else return false.     public boolean offer(E element)     {         return linkedBlockingDeque.offer(element);     }      // method to insert a specified element at the tail of     // this queue. Return true upon successful insertion     // else return false.     public boolean offer(E element, long timeout,                          TimeUnit unit)         throws InterruptedException     {         return linkedBlockingDeque.offer(element, timeout,                                          unit);     }      // method to retrieve element from head of deque .     public E peek() { return linkedBlockingDeque.peek(); }      // method to remove and retrieve element from head of     // deque .     public E poll() { return linkedBlockingDeque.poll(); }      // method to remove and retrieve element from head of     // deque .     public E poll(long timeout, TimeUnit unit)         throws InterruptedException     {         return linkedBlockingDeque.poll(timeout, unit);     }      // method to insert the specified element at the tail of     // the deque.     public void put(E element) throws InterruptedException     {         linkedBlockingDeque.put(element);     }      // method to return the number of additional elements     // that the deque can ideally accept without blocking.     public int remainingCapacity()     {         return linkedBlockingDeque.remainingCapacity();     }      // method to remove a single instance of the     // specified element from the deque     public boolean remove(Object object)     {         return linkedBlockingDeque.remove(object);     }      // method to return the number of elements in the deque.     public int size() { return linkedBlockingDeque.size(); }      // method to retrieve and remove the head of the deque.     public E take() throws InterruptedException     {         return linkedBlockingDeque.take();     }      // method to return an array containing all of     // the elements in the deque.     public Object[] toArray()     {         return linkedBlockingDeque.toArray();     }      // method to return an array containing all of     // the elements in the deque     public <T> T[] toArray(T[] array)     {         return linkedBlockingDeque.toArray(array);     }      // method to return a string representation of the     // collection     public String toString()     {         return linkedBlockingDeque.toString();     }      // method to insert specified element at the front of     // the deque.     public void addFirst(E element)     {         linkedBlockingDeque.addFirst(element);     }      // method to insert specified element at the end of the     // deque.     public void addLast(E element)     {         linkedBlockingDeque.addLast(element);     }      // method to retrieve first element of the deque.     public void getFirst()     {         linkedBlockingDeque.getFirst();     }      // method to retrieve the last element of the deque.     public void getLast() { linkedBlockingDeque.getLast(); }      // Inserts the specified element at the front of this     // deque     public boolean offerFirst(E element)     {         return linkedBlockingDeque.offerFirst(element);     }      // method to insert a specified element at the end of     // the deque.     public boolean offerLast(E element)     {         return linkedBlockingDeque.offerLast(element);     }      // method to retrieve first element of the deque .     public E peekFirst()     {         return linkedBlockingDeque.peekFirst();     }      // method to retrieve last element of the deque .     public E peekLast()     {         return linkedBlockingDeque.peekLast();     } }  public class LinkedBlockingDequeDemo {     public static void main(String[] args)         throws InterruptedException     {                // create object of LinkedBlockingDeque          // using LinkedBlockingDeque() constructor          LinkedBlockingDequeImpl<Integer> linkedBlockingDeque             = new LinkedBlockingDequeImpl<Integer>();                try {                        // Add numbers to end of LinkedBlockingDeque             linkedBlockingDeque.put(10);             linkedBlockingDeque.put(20);             linkedBlockingDeque.put(30);         }                catch (InterruptedException e)         {             e.printStackTrace();         }                System.out.println(             "the elements of the linkedBlockingDeque is ");                Iterator<Integer> itr = linkedBlockingDeque.iterator();                while (itr.hasNext())          {             System.out.print(itr.next() + "\t");         }                System.out.println();                linkedBlockingDeque.offer(60);         linkedBlockingDeque.offer(70);                System.out.println(             "the peak element of the linkedBlockingDeque is(by peeking) "             + linkedBlockingDeque.peek());                System.out.println(             "the peak element of the linkedBlockingDeque is(by polling) "             + linkedBlockingDeque.poll());                System.out.println(             "the remaining capacity is "             + linkedBlockingDeque.remainingCapacity());                System.out.println(             "element 30 removed "             + linkedBlockingDeque.remove(30));                System.out.println(             "the linkedBlockingDeque contains 40 :"             + linkedBlockingDeque.contains(40));                System.out.println(             "the linkedBlockingDeque contains 10 :"             + linkedBlockingDeque.contains(10));                System.out.println(             "the size of the linkedBlockingDeque is "             + linkedBlockingDeque.size());                System.out.println(linkedBlockingDeque);     } } 

Output
the elements of the linkedBlockingDeque is  10    20    30     the peak element of the linkedBlockingDeque is(by peeking) 10 the peak element of the linkedBlockingDeque is(by polling) 10 the remaining capacity is 2147483643 element 30 removed true the linkedBlockingDeque contains 40 :false the linkedBlockingDeque contains 10 :false the size of the linkedBlockingDeque is 3 [20, 60, 70]

 
 


 


Next Article
Java Program to Implement LinkedBlockingDeque API

G

gunjanpaul
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Java Programs
  • Technical Scripter 2020
  • Java-Collections
  • Java-LinkedBlockingDeque
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    Java Program to Implement LinkedBlockingQueue API
    LinkedBlockingQueue API is an optionally-bounded queue based on linked nodes. It orders the elements in FIFO(First In First Out) order. The head of this queue is the element that has been there in the queue for the longest time and the tail of the queue is the element that has been in the queue for
    6 min read
    Java Program to Implement LinkedList API
    Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa
    10 min read
    Java Program to Implement LinkedTransferQueue API
    LinkedTransferQueue is a queue that orders elements FIFO (first-in-first-out) with respect to any given producer. The head of the queue is that element that has been on the queue the longest time for some producer. The tail of the queue is that element that has been on the queue the shortest time fo
    5 min read
    Java Program to Implement PriorityBlockingQueue API
    PriorityBlockingQueue is an unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. The “blocking” part of the name is added to imply the thread will block waiting until there’s an item available on the queue. This class does not
    2 min read
    Java Program to Implement ArrayBlockingQueue API
    ArrayBlockingQueue class is a member of the Java Collection framework. ArrayBlockingQueue is a bounded blocking queue. The term bounded, means that the size of the Queue is fixed and cannot be changed. Any attempt to put element/elements into a full queue will lead to blocking operation. Similarly,
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences