LinkedBlockingQueue size() method in Java


The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

In this article, we are going to learn how to use the LinkedBlockingQueue size() method in Java.

LinkedBlockingQueue is a optionally bounded blocking queue based upon linked nodes. It follows FIFO, means that the oldest inserted element will be at the head of the queue. If capacity is given LinkedBlockingQueue is bounded otherwise it is unbounded.

In this question, we are given a LinkedBlockingQueue. We have to implement the LinkedBlockingQueue size() method.

LinkedBlockingQueue size() method

Syntax - public int size()

Return Value - Returns the number of elements present in the LinkedBlockingQueue.

Suppose the LinkedBlockingQueue is [“Coding”,”is,”fun”]. As there are 3 elements in the LinkedBlockingQueue, the size method will return the value 3.

Input: [“Coding”,”is,”fun”]
Items in the Queue are [Coding, is, fun]
Size of the Queue is 3

Explanation: As there are 3 elements in the LinkedBlockingQueue, the size() method returns 3.

Code Implementation

import java.util.concurrent.LinkedBlockingQueue;
public class PrepBytes {
    public static void main(String[] args)
        throws InterruptedException

        int capacityOfQueue = 3;

        LinkedBlockingQueue LQueue
            = new LinkedBlockingQueue(capacityOfQueue);

        int size = LQueue.size();

        size = LQueue.size();
        System.out.println("Items in the Queue are " + LQueue);
        System.out.println("Size of the Queue is " + size);


Items in the Queue are [Coding, is, fun]
Size of the Queue is 3

Time Complexity: O(1), as all the LinkedBlockingQueue methods other than Remove() operate in constant time.

So, in this article, we have tried to explain how to use LinkedBlockingQueue size() method in Java. This is an important concept when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link Linked List.

Previous post LinkedList add() method in Java
Next post Python Program to find the middle of a linked list using only one traversal

Leave a Reply

Your email address will not be published. Required fields are marked *