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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。