1. poll() Method
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<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) Method
PriorityBlockingQueue的poll(long timeout,TimeUnit unit)方法检索并从此PriorityBlockingQueue的头部删除元素。如果PriorityBlockingQueue为空,则将等待指定的时间以使元素变为可用。等待时间和时间单位为作为方法的参数给出。
用法:
public E poll(long timeout, TimeUnit unit) throws InterruptedException
参数:
此方法接受两个参数:
- timeout(long):放弃前要等待多长时间,以单位为单位。
- unit(TimeUnit):一个TimeUnit确定如何解释超时参数。
返回值:此方法从PriorityBlockingQueue的头部返回元素,如果在元素可用之前经过了指定的等待时间,则返回null。
异常:此方法仅引发一个Exception InterruptedException-如果在等待时被中断
下面的程序说明PriorityBlockingQueue的poll(长时间超时,TimeUnit单位)方法:
范例1:程序演示PriorityBlockingQueue上的poll(长时间超时,TimeUnit单位)方法,以从数字列表中删除元素。
// 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-
相关用法
- Java ConcurrentLinkedQueue poll()用法及代码示例
- Java LinkedTransferQueue poll()用法及代码示例
- Java ArrayDeque poll()用法及代码示例
- Java ArrayBlockingQueue poll()用法及代码示例
- Java PriorityQueue poll()用法及代码示例
- Java LinkedBlockingDeque poll()用法及代码示例
- Java LinkedBlockingQueue poll()用法及代码示例
- Java Queue poll()用法及代码示例
- Java ConcurrentLinkedDeque poll()用法及代码示例
- Java DelayQueue poll()用法及代码示例
- Java BlockingQueue poll()用法及代码示例
- Java BlockingDeque poll()用法及代码示例
- Java PriorityBlockingQueue take()用法及代码示例
- Java PriorityBlockingQueue contains()用法及代码示例
- Java PriorityBlockingQueue put()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 PriorityBlockingQueue poll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。