Java中的 PriorityBlockingQueue poll() 方法
1. poll() 方法
PriorityBlockingQueue 的poll()方法从这个 PriorityBlockingQueue 的头部检索和删除元素。此方法返回它从 PriorityBlockingQueue 中删除的元素,但是当队列为空时,该方法将返回 null。
句法:
public E poll()
返回:此方法返回此 PriorityBlockingQueue 头部的元素,如果此队列为空,则返回 null。
下面的程序说明了 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 PrioQueue
= new PriorityBlockingQueue(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() 方法从 String 值列表中删除 String 并在列表为空时返回 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 names
= new PriorityBlockingQueue(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) : 确定如何解释超时参数的 TimeUnit。
返回:此方法从此 PriorityBlockingQueue 的头部返回元素,如果在元素可用之前经过指定的等待时间,则返回 null。
异常:此方法仅抛出一个异常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 PrioQueue
= new PriorityBlockingQueue(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 Java
- https://docs.oracle.com/javase/8/docs/api/java Java