VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-implement-linkedblockingqueue-api/

⇱ Java Program to Implement LinkedBlockingQueue API - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Implement LinkedBlockingQueue API

Last Updated : 23 Jul, 2025

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 the shortest time.  New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. It belongs to java.util.concurrent package. It extends Object, AbstractCollection and AbstractQueue classes.

The capacity argument serves as a way to prevent excessive queue expansion. The capacity (if not specified) is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity. 

LinkedBlockingQueue API is a member of Java Collection Framework.

All implemented interfaces:

1. Serializable
2. Iterable<E>
3. Collection<E>
4. BlockingQueue<E>
5. Queue<E>

Parameters: E — The type of elements in the collection

Syntax:

public class LinkedBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, Serializable

Constructors:

  1. public LinkedBlockingQueue() - Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE.
  2. public LinkedBlockingQueue(Collection<? extends E> c) - Creates a LinkedBlockingQueue of capacity Integer.MAX_VALUE, initially containing the elements of the specified Collection.
  3. public LinkedBlockingQueue(int capacity) - Creates a LinkedBlockingQueue with the given capacity.

Implementation:

Example


Output
The elements of the LinkedBlockingQueue is 
1 2 3 
The remaining capacity is 2147483644
3 removed true
The peak element of the LinkedBlockingQueue is 1
The peak element of the LinkedBlockingQueue is 1
The LinkedBlockingQueue contains 4 :false
The LinkedBlockingQueue contains 1 :false
The size of the LinkedBlockingQueue is 3
[2, 6, 7]

Methods:


 

MethodTypeDescription
clear()void Removes all the elements of the LinkedBlockingQueue
contains(Object O)booleanReturns true if the queue contains the specified element.
iterator()Iterator(<E>)Returns an iterator over the elements in the queue.
offer(E e)booleanInserts the specified element at the tail of this queue if it is possible and returns true upon success otherwise false.
offer(E e, long timeout, TimeUnit unit)booleanInserts the specified element at the tail of the queue and wait for the specified time for the space to become available.
peek()ERetrieves, but does not remove, the head of the queue.
poll()ERetrieves and removes the head of the queue.
put(E e)voidInserts the specified element at the tail of this queue, waiting if necessary for space to become available.
remainingCapacity()intReturns the number of additional elements that this queue can ideally accept without blocking.
size()intReturns the size of the queue
toArray()Object[]Converts the queue to an array
drainTo(Collection<? super E> c)intRemoves all available elements from the queue and adds them to the specified collection.
take()ERetrieves and removes the head of this queue, waiting if necessary until an element becomes available.
remove(Object O)booleanRemoves the specified element from the queue, if it is present.


 

Comment