ArrayBlockingQueue poll() 方法


Java 中 ArrayBlockingQueue 類的方法 poll() 從此佇列中檢索並移除頭,如果此佇列為空,則返回 null。

語法如下

E poll()

使用 ArrayBlockingQueue 類,需要匯入以下包

import java.util.concurrent.ArrayBlockingQueue;

以下是 Java ArrayBlockingQueue 類的 poll() 方法的實現示例

示例

 線上演示

import java.util.concurrent.ArrayBlockingQueue;
public class Demo {
   public static void main(String[] args) throws InterruptedException {
      ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(10);
      q.add(200);
      q.add(310);  
      q.add(400);
      q.add(450);
      q.add(500);
      q.add(550);
      q.add(700);
      System.out.println("ArrayBlockingQueue = " + q);
      System.out.println("Head of the queue = "+q.poll());
      System.out.println("Head of the queue now = "+q.poll());
      System.out.println("ArrayBlockingQueue = " + q);
   }
}

輸出

ArrayBlockingQueue = [200, 310, 400, 450, 500, 550, 700]
Head of the queue = 200
Head of the queue now = 310
ArrayBlockingQueue = [400, 450, 500, 550, 700]

更新於: 30-7-2019

126 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始學習
廣告