LinkedBlockingQueue Class in Java


The LinkedBlockingQueue Class in Java has a blocking queue that is optionally bounded and based on linked nodes. This means that if the capacity is provided then the LinkedBlockingQueue is bound, otherwise it is not bound. Also, FIFO for ordering elements.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.util.concurrent.LinkedBlockingQueue;
public class Demo {
   public static void main(String[] args) {
      LinkedBlockingQueue<String> lbQueue = new LinkedBlockingQueue<String>();
      lbQueue.add("Amy");
      lbQueue.add("John");
      lbQueue.add("May");
      lbQueue.add("Harry");
      lbQueue.add("Anne");
      System.out.println("The elements in LinkedBlockingQueue are: " + lbQueue);
   }
}

The output of the above program is as follows −

Output

The elements in LinkedBlockingQueue are: [Amy, John, May, Harry, Anne]

Now let us understand the above program.

The LinkedBlockingQueue is created and then elements are added to it. Finally it is displayed. A code snippet that demonstrates this is given as follows −

LinkedBlockingQueue<String> lbQueue = new LinkedBlockingQueue<String>();
lbQueue.add("Amy");
lbQueue.add("John");
lbQueue.add("May");
lbQueue.add("Harry");
lbQueue.add("Anne");
System.out.println("The elements in LinkedBlockingQueue are: " + lbQueue);

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements