实现PriorityBlockingQueue API的Java程序
PriorityBlockingQueue 是一个无界阻塞队列,它使用与 PriorityQueue 类相同的排序规则并提供阻塞检索操作。添加名称的“阻塞”部分以暗示线程将阻塞等待,直到队列中有可用的项目。此类不允许空元素。依赖于自然排序的优先级队列也不允许插入不可比较的对象(这样做会导致 ClassCastException)。
它实现了 Serializable、Iterable
宣言:
public class PriorityBlockingQueue extends AbstractQueue implements BlockingQueue, Serializable
// Here, E is the type of elements held in this collection
java.lang.Object
java.util.AbstractCollection
java.util.AbstractQueue
java.util.concurrent.PriorityBlockingQueue
执行:
例子
Java
// Java Program to Implement PriorityBlockingQueue API
// Importing concurrent classes from
// java.util package
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
// Class for PriorityQueue
public class GFG {
// Main driver method
public static void main(String[] args)
throws InterruptedException
{
// Creating a new object of PriorityBlockingQueue
// Declaring Integer type object
PriorityBlockingQueue priorityBlockingQueue
= new PriorityBlockingQueue<>();
// Creation of a thread
new Thread(() -> {
// Display message
System.out.println("Waiting to poll ...");
// Try block to check for exceptions
try {
// Condition check
while (true) {
// Return (integer) value at head of
// queue of PriorityBlockingQueue
Integer poll
= priorityBlockingQueue.take();
// Display and print element returned in
// PriorityBlockingQueue
System.out.println("Polled : " + poll);
// Pause the execution of current thread
// for certain amount of time using
// toMills() method() to showcase
// working of PriorityBlockingQueue
Thread.sleep(
TimeUnit.SECONDS.toMillis(1));
}
}
// Catch block to handle exceptions if any
catch (InterruptedException e) {
// Print and display the line number
// where exception/s occured
e.printStackTrace();
}
// Execution of thread begins with
// use of start() method
}).start();
// Custom elememts inputs
// 1, 2, 3 to priorityBlockingQueue
// Pausing execution of first thread
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
// Insert parameter element-> 1 to method
// at the tail of priority queue
priorityBlockingQueue.add(1);
// Pausing execution of second thread
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
// Insert parameter element-> 2 to method
// at the tail of priority queue
priorityBlockingQueue.add(2);
// pausing execution of third thread
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
// Insert parameter element-> 3 to method
// at the tail of priority queue
priorityBlockingQueue.add(3);
}
}
输出: