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


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


ArrayBlockingQueue是有界的阻塞隊列,該隊列內部使用數組支持的元素存儲。

  • ArrayBlockingQueue類是Java Collections Framework的成員。
  • 有界意味著它將具有固定的大小,您不能存儲數量超過隊列容量的元素。
  • 隊列還遵循FIFO(先進先出)規則,用於存儲和刪除隊列中的元素。
  • 如果您嘗試將一個元素放入一個完整的隊列或從一個空隊列中取出一個元素,那麽該隊列將阻塞。

poll()方法有兩種類型,具體取決於傳遞的參數數。

  1. poll()方法檢索並從此隊列的開頭刪除元素。如果隊列為空,則方法將返回null。
    用法:
    public E poll()

    返回值:該方法從此隊列的開頭返回元素;如果此隊列為空,則返回null。

    下麵的程序說明ArrayBlockingQueue的poll()方法。

    示例1:

    /* 
    *Program Demonstrate poll() method of ArrayBlockingQueue. 
    */
      
    import java.util.concurrent.ArrayBlockingQueue; 
      
    public class GFG { 
      
        public static void main(String[] args) 
        { 
            // define capacity of ArrayBlockingQueue 
            int capacity = 5; 
      
            // create object of ArrayBlockingQueue 
            ArrayBlockingQueue<Integer> queue = new 
                   ArrayBlockingQueue<Integer>(capacity); 
      
            // Add elements to ArrayBlockingQueue 
            queue.offer(423); 
            queue.offer(233); 
            queue.offer(356); 
      
            // print elements 
            System.out.println("Queue Contains" + queue); 
      
            // try to poll  elements 
            System.out.println("Removing From head: " +  
                                           queue.poll()); 
            System.out.println("Queue Contains" + queue); 
            System.out.println("Removing From head: " +  
                                           queue.poll()); 
            System.out.println("Queue Contains" + queue); 
            System.out.println("Removing From head: " +  
                                           queue.poll()); 
            System.out.println("Queue Contains" + queue); 
            System.out.println("Removing From head: " +  
                                           queue.poll()); 
            System.out.println("Queue Contains" + queue); 
        } 
    }
    輸出:
    Queue Contains[423, 233, 356]
    Removing From head: 423
    Queue Contains[233, 356]
    Removing From head: 233
    Queue Contains[356]
    Removing From head: 356
    Queue Contains[]
    Removing From head: null
    Queue Contains[]
    

    示例2:

    /* 
    * Program Demonstrate poll() method of ArrayBlockingQueue. 
    */
      
    import java.util.concurrent.ArrayBlockingQueue; 
      
    public class GFG { 
      
        // Create a User Object with name and age as an attribute 
        public class User { 
      
            public String name; 
            public String age; 
            User(String name, String age) 
            { 
                this.name = name; 
                this.age = age; 
            } 
        } 
      
        // Main Method 
        public static void main(String[] args) 
        { 
            GFG gfg = new GFG(); 
            gfg.pollMethodExample(); 
        } 
      
        // Method to give example of contains function 
        public void pollMethodExample() 
        { 
      
            // Define the capacity of ArrayBlockingQueue 
            int capacity = 5; 
      
            // Create object of ArrayBlockingQueue 
            ArrayBlockingQueue<User> queue =  
                   new ArrayBlockingQueue<User>(capacity); 
      
            // Create user objects 
            User user1 = new User("Aman", "24"); 
            User user3 = new User("Sanjeet", "25"); 
      
            // Add Objects to ArrayBlockingQueue 
            queue.offer(user1); 
            queue.offer(user3); 
      
            // Poll users from queue 
            User user = queue.poll(); 
            System.out.println("removing user having name = "
                                                + user.name); 
            user = queue.poll(); 
            System.out.println("removing user having name = "
                                                + user.name); 
      
            // Now queue is empty 
            // Try to remove it will return null 
            user = queue.poll(); 
            System.out.println("removing user having name = "
                                                     + user); 
        } 
    }
    輸出:
    removing user having name = Aman
    removing user having name = Sanjeet
    removing user having name = null
    
  2. poll(long timeout,TimeUnit unit)方法檢索並從此隊列的開頭刪除元素。如果隊列為空,那麽它將等待,直到指定的時間,元素才可用。
    用法:
    public E poll(long timeout, TimeUnit unit) throws InterruptedException

    參數:該方法有兩個參數:

    • timeout(長)–放棄之前要等待的時間,以單位為單位。
    • unit(TimeUnit)-確定如何解釋超時參數的TimeUnit。

    返回值:該方法返回此隊列的開頭;如果在元素可用之前經過了指定的等待時間,則返回null。
    異常:如果在等待時被中斷,該方法將拋出InterruptedException。

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

    /* 
    * Program Demonstrate offer(E e, long timeout, TimeUnit unit) 
    * method of ArrayBlockingQueue. 
    */
      
    import java.util.concurrent.ArrayBlockingQueue; 
    import java.util.concurrent.TimeUnit; 
      
    public class GFG { 
      
        public static void main(String[] args)  
                           throws InterruptedException 
        { 
            // Define capacity of ArrayBlockingQueue 
            int capacity = 5; 
      
            // Create object of ArrayBlockingQueue 
            ArrayBlockingQueue<Integer> queue =  
                new ArrayBlockingQueue<Integer>(capacity); 
      
            // Add elements to ArrayBlockingQueue 
            queue.offer(423); 
            queue.offer(233); 
            queue.offer(356); 
      
            // Print elements 
            System.out.println("Queue Contains" + queue); 
      
            // Try to poll  elements 
            System.out.println("Removing From head: " 
                      + queue.poll(10, TimeUnit.SECONDS)); 
            System.out.println("Queue Contains" + queue); 
            System.out.println("Removing From head: " 
                      + queue.poll(10, TimeUnit.SECONDS)); 
            System.out.println("Queue Contains" + queue); 
            System.out.println("Removing From head: " +  
                        queue.poll(10, TimeUnit.SECONDS)); 
            System.out.println("Queue Contains" + queue); 
            System.out.println("Removing From head: " +  
                       queue.poll(10, TimeUnit.SECONDS)); 
        } 
    }
    輸出:
    Queue Contains[423, 233, 356]
    Removing From head: 423
    Queue Contains[233, 356]
    Removing From head: 233
    Queue Contains[356]
    Removing From head: 356
    Queue Contains[]
    Removing From head: null
    

參考:
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#poll()



相關用法


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