当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java PriorityBlockingQueue poll()用法及代码示例


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

参数:
此方法接受两个参数:

  1. timeout(long):放弃前要等待多长时间,以单位为单位。
  2. 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[]

参考:



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 PriorityBlockingQueue poll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。