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


Java BlockingQueue poll()用法及代碼示例


BlockingQueue接口的poll(long timeout,TimeUnit unit)方法通過從隊列中刪除該元素來返回BlockingQueue的頭部。可以說此方法從此LinkedBlockingQueue的頭部檢索並刪除了元素。如果隊列為空,則poll()方法將等待直到指定時間元素可用。

用法:

public E poll(long timeout, TimeUnit unit) throws 

參數:此方法采用兩個強製性參數:


  • timeout–等待的時間,以單位為單位。
  • unit–超時參數的TimeUnit。

返回值:此方法從此LinkedBlockingQueue的頭部檢索並刪除元素,如果在元素可用之前經過了指定的等待時間,則為null。

異常如果在等待元素可用時方法被中斷,則此方法將引發InterruptedException。

注意:BlockingQueue的poll()方法已從Java中的Queue類繼承。

以下示例程序旨在說明BlockingQueue的poll(長時間超時,TimeUnit單位)方法:

程序1

// Java program to demonstrate 
// poll(long timeout, TimeUnit unit) 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
import java.util.concurrent.BlockingQueue; 
import java.util.concurrent.TimeUnit; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        // define capacity of BlockingQueue 
        int capacityOfQueue = 4; 
  
        // create object of BlockingQueue 
        BlockingQueue<String> BQ 
            = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // Add element to BlockingQueue 
        BQ.add("Ravi"); 
        BQ.add("Suraj"); 
        BQ.add("Harsh"); 
  
        // print elements of queue 
        System.out.println("Items in Queue are " + BQ); 
  
        // Try to poll elements from BQ 
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS)); 
  
        // print queue details 
        System.out.println("Now Queue Contains" + BQ); 
  
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS)); 
  
        // print queue details 
        System.out.println("Now Queue Contains" + BQ); 
  
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS)); 
  
        // print queue details 
        System.out.println("Now Queue Contains" + BQ); 
  
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + BQ.poll(5, TimeUnit.SECONDS)); 
        // print queue details 
        System.out.println("Now Queue Contains" + BQ); 
    } 
}
輸出:
Items in Queue are [Ravi, Suraj, Harsh]
Removing item From head: Ravi
Now Queue Contains[Suraj, Harsh]
Removing item From head: Suraj
Now Queue Contains[Harsh]
Removing item From head: Harsh
Now Queue Contains[]
Removing item From head: null
Now Queue Contains[]

程序2

// Java program to demonstrate 
// poll(long timeout, TimeUnit unit) 
// method of LinkedBlockingQueue 
  
import java.util.concurrent.LinkedBlockingQueue; 
import java.util.concurrent.BlockingQueue; 
import java.util.concurrent.TimeUnit; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws InterruptedException 
    { 
        // define capacity of BlockingQueue 
        int capacityOfQueue = 2; 
  
        // create object of BlockingQueue 
        BlockingQueue<String> BQ 
            = new LinkedBlockingQueue<String>(capacityOfQueue); 
  
        // Add element to BlockingQueue 
        BQ.add("Gopal"); 
        BQ.add("GFG"); 
  
        // print elements of queue 
        System.out.println("Items in Queue are " + BQ); 
  
        // Try to poll elements from BQ 
        // using poll(long timeout, TimeUnit unit) method 
        System.out.println("Removing item From head: "
                           + BQ.poll(2, TimeUnit.SECONDS)); 
  
        // print queue details 
        System.out.println("Now Queue Contains" + BQ); 
    } 
}
輸出:
Items in Queue are [Gopal, GFG]
Removing item From head: Gopal
Now Queue Contains[GFG]

參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html#poll(long, %20java.util.concurrent.TimeUnit)



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 BlockingQueue poll() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。