當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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