Java PriorityBlockingQueue poll()方法
1.poll()方法
PriorityBlockingQueue的 poll() 方法 从 这个PriorityBlockingQueue的 头部检索和删除元素 。这个方法返回它从PriorityBlockingQueue中移除的元素,但是当队列是空的时候,方法将返回null。
语法
public E poll()
返回: 该方法返回来自该PriorityBlockingQueue头部的元素,如果该队列是空的,则返回空。
下面的程序说明了PriorityBlockingQueue的poll()方法。
例1: 演示PriorityBlockingQueue的poll()方法的程序,从一个数字列表中删除一个元素。
// Java Program Demonstrate poll()
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
// Add numbers to PriorityBlockingQueue
PrioQueue.offer(35658786);
PrioQueue.offer(5278367);
PrioQueue.offer(74381793);
PrioQueue.offer(87625142);
// remove numbers from head using poll()
// and print removed number
int removedItem = PrioQueue.poll();
// print details
System.out.println("Removed Element: " + removedItem);
System.out.println("Now Queue Contains:");
System.out.println(PrioQueue.toString());
}
}
输出:
Removed Element: 5278367
Now Queue Contains:
[35658786, 87625142, 74381793]
例2: 演示poll()方法的程序,从一个字符串列表中删除字符串,如果列表为空,则返回null。
// Java Program Demonstrate poll()
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue which contains
// name of students
PriorityBlockingQueue<String> names
= new PriorityBlockingQueue<String>(capacityOfQueue);
// Add names of students of girls college
names.offer("Joyita");
names.offer("Priyanka");
// remove two names from list of names
// and print removed name
String removedName1 = names.poll();
String removedName2 = names.poll();
// print details
System.out.println("Removed Name 1: " + removedName1);
System.out.println("Removed Name 2: " + removedName2);
System.out.println("Now Queue Contains:");
System.out.println(names.toString());
// try to remove from empty PriorityBlockingQueue
String removedName3 = names.poll();
System.out.println("Removed Name 3: " + removedName3);
}
}
输出:
Removed Name 1: Joyita
Removed Name 2: Priyanka
Now Queue Contains:
[]
Removed Name 3: null
2. poll(long timeout, TimeUnit unit) 方法
PriorityBlockingQueue的 poll(long timeout, TimeUnit unit) 方法 从 这个PriorityBlockingQueue的 头部检索并移除元素 。如果PriorityBlockingQueue是空的,那么它将等待到指定的时间,让一个元素变得可用。
语法
public E poll(long timeout, TimeUnit unit) throws InterruptedException
参数:
这个方法接受两个参数。
- timeout(long) : 在放弃之前要等待多长时间,单位为unit。
- unit(TimeUnit) : 决定如何解释timeout参数的TimeUnit。
返回: 该方法返回该PriorityBlockingQueue头部的元素,如果在指定的等待时间过后才有元素,则返回空。
异常: 该方法只抛出一个异常 InterruptedException --如果在等待时被打断。
下面的程序说明了PriorityBlockingQueue的poll(long timeout, TimeUnit unit) 方法。
例1: 演示PriorityBlockingQueue的poll(long timeout, TimeUnit unit)方法的程序,从一个数字列表中删除一个元素。
// Java Program Demonstrate poll(long timeout, TimeUnit unit)
// method of PriorityBlockingQueue
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String[] args)
throws InterruptedException
{
// define capacity of PriorityBlockingQueue
int capacityOfQueue = 5;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<Integer> PrioQueue
= new PriorityBlockingQueue<Integer>(capacityOfQueue);
// Add numbers to PriorityBlockingQueue
PrioQueue.offer(35658786);
PrioQueue.offer(5278367);
// Try to poll elements from PriorityBlockingQueue
System.out.println("Removed Number: "
+ PrioQueue.poll(10, TimeUnit.SECONDS));
System.out.println("List Contains" + PrioQueue);
System.out.println("Removed Number: "
+ PrioQueue.poll(10, TimeUnit.SECONDS));
System.out.println("List Contains" + PrioQueue);
System.out.println("Removed Number: "
+ PrioQueue.poll(10, TimeUnit.SECONDS));
System.out.println("List Contains" + PrioQueue);
}
}
输出:
Removed Number: 5278367
List Contains[35658786]
Removed Number: 35658786
List Contains[]
Removed Number: null
List Contains[]
参考资料
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#poll-
- https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#poll-long-java.util.concurrent.TimeUnit-