📜  Java中的 ArrayBlockingQueue poll() 方法

📅  最后修改于: 2022-05-13 01:55:23.047000             🧑  作者: Mango

Java中的 ArrayBlockingQueue poll() 方法

ArrayBlockingQueue是有界的阻塞队列,它在内部存储由数组支持的元素。

  • ArrayBlockingQueue类是Java集合框架的成员。
  • 有界意味着它将具有固定大小,您不能存储数量超过队列容量的元素。
  • 队列还遵循 FIFO(先进先出)规则来存储和删除队列中的元素。
  • 如果你试图将一个元素放入一个满队列或从一个空队列中取出一个元素,那么队列会阻止你。

根据传递的参数数量,有两种 poll() 方法。

  1. poll()方法从该队列的头部检索并删除元素。如果队列为空,则该方法将返回 null。
    句法:
    public E poll()

    返回值:该方法返回此队列头部的元素,如果此队列为空,则返回 null。

    下面的程序说明了 ArrayBlockingQueue 的 poll() 方法。

    方案一:

    /*
    *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 queue = new 
                   ArrayBlockingQueue(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[]
    

    方案二:

    /*
    * 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 queue = 
                   new ArrayBlockingQueue(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 (long) – 在放弃之前等待多长时间,以 unit 为单位。
    • 单位(TimeUnit) - 确定如何解释超时参数的 TimeUnit。

    返回值:该方法返回此队列的头部,如果在元素可用之前经过指定的等待时间,则返回 null。
    异常:如果在等待时被中断,该方法将抛出InterruptedException

    下面的程序说明了 ArrayBlockingQueue 的 poll(long timeout, TimeUnit unit) 方法。

    /*
    * 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 queue = 
                new ArrayBlockingQueue(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 Java()