📜  Java中的 ArrayBlockingQueue offer() 方法

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

Java中的 ArrayBlockingQueue offer() 方法

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

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

根据传递给它的参数,有两种类型的 offer() 方法:

  1. 如果队列未满, offer(E element )方法将作为参数传递给方法的元素插入此队列(ArrayBlockingQueue) 的尾部。添加操作成功时返回true,如果此队列已满则返回false。此方法优于 add() 方法,因为 add() 方法在队列已满时抛出错误,但 offer() 方法在这种情况下返回 false。
    句法:
    public boolean offer(E e)

    参数:该方法采用一个参数element 。这是指要添加到队列中的元素。

    返回值:该方法在添加操作成功时返回True ,如果该队列已满则返回false。

    异常如果指定元素为空,该方法将抛出NullPointerException

    下面的程序说明了 ArrayBlockingQueue 的 offer(E element) 方法:
    方案一:

    // Demonstrating offer(E element) 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 5 elements to ArrayBlockingQueue
        System.out.println("adding 423: "+queue.offer(423));
        System.out.println("adding 243: "+queue.offer(243));
        System.out.println("adding 237: "+queue.offer(237));
        System.out.println("adding 867: "+queue.offer(867));
        System.out.println("adding 23: "+queue.offer(23));
          
        // Check whether queue is full or not
        if(queue.remainingCapacity()==0) {
            System.out.println("queue is full");
            System.out.println("queue contains "+queue);
        }else {
            System.out.println("queue is not full");
            System.out.println("queue contains "+queue);
        }
          
        // Try to add more elements
        System.out.println("adding 123: "+queue.offer(123));
        System.out.println("adding 321: "+queue.offer(321));
          
    } 
    }
    
    输出:
    adding 423: true
    adding 243: true
    adding 237: true
    adding 867: true
    adding 23: true
    queue is full
    queue contains [423, 243, 237, 867, 23]
    adding 123: false
    adding 321: false
    

    方案二:

    // Program Demonstrate offer(E e) 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.offerExample();
        }
      
        // Method to give example of contains function
        public void offerExample()
        {
      
            // 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 user2 = new User("Amar", "23");
            User user3 = new User("Sanjeet", "25");
            User user4 = new User("Suvo", "26");
            User user5 = new User("Ravi", "22");
      
            // Add Objects to ArrayBlockingQueue
            System.out.println("adding user having name = "
                    + user1.name + ": " + queue.offer(user1));
            System.out.println("adding user having name = "
                    + user2.name + ": " + queue.offer(user2));
            System.out.println("adding user having name = "
                    + user3.name + ": " + queue.offer(user3));
            System.out.println("adding user having name = "
                    + user4.name + ": " + queue.offer(user4));
            System.out.println("adding user having name = "
                    + user5.name + ": " + queue.offer(user5));
      
            // Check whether the queue is full or not
            if (queue.remainingCapacity() == 0) {
                System.out.println("queue is full");
            }
            else {
                System.out.println("queue is not full");
            }
      
            // Create more user objects
            User user6 = new User("Ram", "20");
            User user7 = new User("Mohan", "27");
      
            // Add users in queue
            System.out.println("adding user having name = "
                    + user6.name + ": " + queue.offer(user6));
            System.out.println("adding user having name = "
                    + user7.name + ": " + queue.offer(user7));
        }
    }
    
    输出:
    adding user having name = Aman: true
    adding user having name = Amar: true
    adding user having name = Sanjeet: true
    adding user having name = Suvo: true
    adding user having name = Ravi: true
    queue is full
    adding user having name = Ram: false
    adding user having name = Mohan: false
    
  2. 如果队列未满,offer( E element, long timeout, TimeUnit unit )方法会将作为参数传递给方法的元素插入到此队列(ArrayBlockingQueue) 的尾部。如果队列已满,它将等到指定的时间才有空间可用。添加操作成功时返回 true,如果此队列已满且经过指定的等待时间后空间可用,则返回 false。当我们想等待队列移除它的元素时,这个方法很有用,当队列未满时,元素将被添加到队列中,但我们可以等待一个特定的时间,这个时间可以由我们定义。

    句法:

    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException

    参数:该方法接受三个参数:

    • element (Object) - 要添加到队列中的元素。
    • timeout (long) - 放弃前等待多长时间,以unit为单位。
    • 单位(TimeUnit) - 确定如何解释超时参数的 TimeUnit。

    返回值:添加方法成功时该方法返回True,如果在空间可用之前经过指定的等待时间则返回false。

    异常:该方法抛出两种类型的异常:

    • InterruptedException – 如果在等待时被中断。
    • NullPointerException – 如果指定元素为空。

    下面的程序说明了ArrayBlockingQueue的offer(E元素,长超时,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 queue = 
                new ArrayBlockingQueue(capacity);
      
            // Add 5 elements to ArrayBlockingQueue having
            // Timeout in seconds with value 10 secs
            System.out.println("adding 423: " + 
                  queue.offer(433, 10, TimeUnit.SECONDS));
            System.out.println("adding 456: " + 
                  queue.offer(456, 10, TimeUnit.SECONDS));
            System.out.println("adding 987: " + 
                  queue.offer(987, 10, TimeUnit.SECONDS));
            System.out.println("adding 578: " + 
                  queue.offer(578, 10, TimeUnit.SECONDS));
            System.out.println("adding 852: " + 
                  queue.offer(852, 10, TimeUnit.SECONDS));
      
            // Check whether queue is full or not
            if (queue.remainingCapacity() == 0) {
                System.out.println("queue is full");
                System.out.println("queue contains " + queue);
            }
            else {
                System.out.println("queue is not full");
                System.out.println("queue contains " + queue);
            }
      
            // Try to add more elements
            System.out.println("adding 546: " + 
                      queue.offer(546, 10, TimeUnit.SECONDS));
        }
    }
    
    输出:
    adding 423: true
    adding 456: true
    adding 987: true
    adding 578: true
    adding 852: true
    queue is full
    queue contains [433, 456, 987, 578, 852]
    adding 546: false
    

参考: https: Java/util/concurrent/ArrayBlockingQueue.html#offer(E)