This example demonstrate how to use the PriorityBlockingQueue
class. The PriorityBlockingQueue
is one implementation of the BlockingQueue
interface. It is an unbounded concurrent queue. The object place in this type of queue must implements the java.lang.Comparable
interface. The Comparable
interface defines how the order priority of the elements inside this queue.
For simplicity, in this example we use strings object as the elements to be placed in the queue. The String
class implements the comparable interface. When we run this example it will print out the names in the string array in alphabetical orders.
package org.kodejava.example.util.concurrent;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
public class PriorityBlockingQueueExample {
public static void main(String[] args) {
final String[] names =
{"carol", "alice", "malory", "bob", "alex", "jacobs"};
final BlockingQueue<String> queue = new PriorityBlockingQueue<>();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < names.length; i++) {
try {
queue.put(names[i]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "Producer").start();
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < names.length; i++) {
System.out.println(queue.take());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "Consumer").start();
}
}
This code print out:
alex
alice
bob
carol
jacobs
malory
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020