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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。