1.poll()方法
这个 投票 方法 优先阻塞队列 从头部检索并删除元素 优先封锁队列。该方法返回它从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.轮询(长超时,时间单位)方法
这个 轮询(长超时,时间单位) 方法 优先阻塞队列 从头部检索并删除元素 优先封锁队列。如果PriorityBlockingQueue为空,则它将等待指定的时间,直到元素变为可用。等待时间和时间单位作为该方法的参数。
语法:
public E poll(long timeout, TimeUnit unit) throws InterruptedException
参数: 此方法接受两个参数:
- 超时(长) :放弃前等待的时间,单位为。
- 单位(时间单位) :确定如何解释超时参数的时间单位。
返回: 此方法从PriorityBlockingQueue的头返回元素,如果指定的等待时间已过,则返回null,然后元素才可用。
例外情况: 此方法只引发一个异常 中断异常 –如果在等待时被打断
下面的程序演示了PriorityBlockingQueue的轮询(长超时、时间单位)方法:
例1: 程序演示PriorityBlockingQueue上的poll(长超时,时间单位)方法,以从数字列表中删除元素。
// 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-长爪哇。util。同时发生的时间单位-