Table of contents
1.
Introduction
2.
Linked Blocking Queue
2.1.
Syntax
3.
Hierarchy
4.
Constructors of LinkedBlockingQueue
4.1.
Example 1
4.2.
Example 2
4.3.
Example 3
5.
Methods of Linked Blocking Queue
6.
Basic Operations
6.1.
Adding Elements
6.1.1.
Code
6.2.
Removing Elements
6.2.1.
Code
7.
FAQs
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linked Blocking Queue

Introduction

This article will discuss the LinkedBlocking Queue and move to the actual topic. We need to brush up on such a topic as Queue. So a Queue can be expressed as a linear data structure that follows a particular order in which the operations are performed. We will be learning about Linked Blocking Queue and its different types of implementation.

Also see,  Swap Function in Java

Linked Blocking Queue

A linked Blocking Queue can be defined as an optionally bounded blocking queue based on linked nodes. These Linked Blocking queues can be bounded if the capacity is mentioned. Otherwise, they will be strictly bounded. The capacity can be defined as a parameter to the constructor. This depends on queue order elements FIFO(first-in-first-out). It means the head of the Queue will be the oldest element present in the Queue. While the tail of the Queue will be the newest element added.

Syntax

public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
You can also try this code with Online Java Compiler
Run Code

Hierarchy

 

Constructors of LinkedBlockingQueue

LinkedBlockingQueue(): This will create a BlockingQueue with an initial capacity of Integer.MAX_VALUE.

LinkedBlockingQueue<E> lbq = new LinekedBlockingQueue<E>();
You can also try this code with Online Java Compiler
Run Code

Example 1

import java.util.concurrent.LinkedBlockingQueue;
public class LinkedBlocking {


public static void main(String[] args) {
     LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>();
    
        lbq.add(1);
        lbq.add(2);
        lbq.add(3);
        lbq.add(4);
        lbq.add(5);
    
        System.out.println("LinkedBlockingQueue:" + lbq);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

LinkedBlockingQueue:[1, 2, 3, 4, 5]
You can also try this code with Online Java Compiler
Run Code

We created a Linked Blocking Queue in this article, added some integers, and printed the output.
 

LinkedBlockingQueue(int capacity): This will create a LinkedBlocklingQueue with the given fixed capacity passed as a parameter.

LinkedBlockingQueue<E> lbq = new LinkedBlockingQueue(int capacity);
You can also try this code with Online Java Compiler
Run Code

Example 2

import java.util.concurrent.LinkedBlockingQueue;
public class LinkedBlockingAdd {


    public static void main(String[] args) {
int capacity = 15;   
        
LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>(capacity);
       
     lbq.add(10);
     lbq.add(20);
     lbq.add(30);
       
             
     System.out.println("LinkedBlockingQueue:" + lbq);
}
}
You can also try this code with Online Java Compiler
Run Code

Output

LinkedBlockingQueue:[10, 20, 30]
You can also try this code with Online Java Compiler
Run Code

We created a Linked Blocking Queue with a specified initial capacity, added some integers, and printed the output in this article. Try it on java online compiler.

 

LinkedBlockingQueue(Collection<? Extends E> c): This constructor will create a Linked Blocking Queue with Integer capacity.MAXVALUE.

LinkedBlckingQueue<E> lbq = new LinkedBlockingQueue(Collection<? Extends E>c);
You can also try this code with Online Java Compiler
Run Code

Example 3

import java.util.concurrent.LinkedBlockingQueue;
import java.util.*;
public class LinkedBlockingAddElement {


public static void main(String[] args) {
        Vector<Integer> v = new Vector<Integer>();
        v.addElement(1);
        v.addElement(2);
        v.addElement(3);
        v.addElement(4);
        v.addElement(5);
  
        LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>(v);


        System.out.println("LinkedBlockingQueue:" + lbq);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

LinkedBlockingQueue:[1, 2, 3, 4, 5]
You can also try this code with Online Java Compiler
Run Code

In this example, we have created a Linked Blocking Queue and used some methods to add the elements and print the output.

Also see, Hashcode Method in Java

Methods of Linked Blocking Queue

Basic Operations

Adding Elements

The function add(E e) in the Linked Blocking Queue which inserts the element passed as a parameter. It adds the elements at the tail of this Linked Blocking Queue.

Code

import java.util.concurrent.LinkedBlockingQueue;
public class AddElements {


public static void main(String[] args) {
int capacity = 10;
  
    LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>(capacity);
  
        lbq.add(1);
        lbq.add(2);
        lbq.add(3);

        System.out.println("LinkedBlockingQueue:" + lbq);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

LinkedBlockingQueue:[1, 2, 3]
You can also try this code with Online Java Compiler
Run Code

In this example, we have used an inbuilt method to add the elements in the linked blocking queue.

Removing Elements

The remove(Object e) is the method that removes only one instance of the given object which is passed as a parameter.

Code

import java.util.concurrent.LinkedBlockingQueue;
public class RemoveElements {


public static void main(String[] args) {
int capacity = 15;


        LinkedBlockingQueue<Integer> lbq = new LinkedBlockingQueue<Integer>(capacity);
  
        lbq.add(1);
        lbq.add(2);
        lbq.add(3);


        System.out.println("LinkedBlockingQueue:" + lbq);


        lbq.clear();


        System.out.println("LinkedBlockingQueue:" + lbq);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

LinkedBlockingQueue:[1, 2, 3]
LinkedBlockingQueue:[]
You can also try this code with Online Java Compiler
Run Code

In this example, we have used an inbuilt function to add the elements and to remove the elements.

Check out this problem - Queue Implementation

FAQs

  1. What is LinkedBlockingQueue in Java?
    It is an optionally bounded blocking queue that is based on Linked nodes.
     
  2. What is a blocking queue in Java?
    A blocking queue indicates that the Queue accessing the thread is full or becomes empty.
     
  3. What happens when a thread is blocked in a Queue?
    A thread tries to enqueue an element in a full queue that is blocked until some or another thread makes space in the Queue.

Conclusion

In this article, we have explained Linked Blocking Queue. We have briefly explained the topic and various other topics which might be needed to begin the article. We have also discussed its syntax and several examples and methods with its description. We hope this blog might have helped you enhance your knowledge of Linked Blocking Queue. If you want to learn more about such topics, please visit Introduction to QueueProblems on Queue. We hope that this blog might help you in enhancing your knowledge. If you liked this article, please give it a thumbs up, which might help me and other ninjas grow. "Happy Coding!".

Live masterclass