Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
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); Advertisements
