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


Java ArrayBlockingQueue offer()用法及代码示例


ArrayBlockingQueue是有界的阻塞队列,该队列内部使用数组支持的元素存储。

  • ArrayBlockingQueue类是Java Collections Framework的成员。
  • 有界意味着它将具有固定的大小,您不能存储数量超过队列容量的元素。
  • 队列还遵循FIFO(先进先出)规则,用于存储和删除队列中的元素。
  • 如果您尝试将一个元素放入一个完整的队列或从一个空队列中取出一个元素,那么该队列将阻塞。

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

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

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

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

    异常如果指定的元素为null,则此方法将引发NullPointerException。

    以下示例程序旨在说明ArrayBlockingQueue的offer(E元素)方法:
    示例1:

    // 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<Integer> queue =  
              new ArrayBlockingQueue<Integer>(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
    

    示例2:

    // 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<User> queue =  
                    new ArrayBlockingQueue<User>(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(对象)-要添加到队列中的元素。
    • timeout(long)-放弃之前要等待的时间,以单位为单位。
    • unit(TimeUnit)-确定如何解释超时参数的TimeUnit。

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

    异常:该方法引发两种类型的异常:

    • InterruptedException–如果在等待时被打断。
    • NullPointerException –如果指定的元素为null。

    以下示例程序旨在说明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<Integer> queue =  
                new ArrayBlockingQueue<Integer>(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://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#offer(E)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ArrayBlockingQueue offer() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。