Open In App

ArrayBlockingQueue offer() Method in Java

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array.

  • ArrayBlockingQueue class is a member of the Java Collections Framework.
  • Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue.
  • The queue also follows FIFO (first-in-first-out) rule for storing and removing elements from the queue.
  • If you try to put an element into a full queue or to take an element from an empty queue then the queue will block you.

There are two types of offer() method depending upon parameter passed to it:

  1. The offer(E element) method inserts the element passed as parameter to method at the tail of this queue(ArrayBlockingQueue), if queue is not full. It returns true when the operation of addition is successful and false if this queue is full. This method is preferred over add() method because add method throws error when queue is full but offer() method returns false in such situation.
    Syntax:

    public boolean offer(E e)

    Parameter: The method takes one parameter, element. This refers to the element to be added in the queue.

    Return Value: This method returns True when the addition operation is successful and false if this queue is full.

    Exception The method throws NullPointerException if the specified element is null.

    Below programs illustrate offer(E element) method of ArrayBlockingQueue:
    Program 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));
          
    }

    
    

    Output:

    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 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 offer 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));
        }
    }

    
    

    Output:

    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. The offer(E element, long timeout, TimeUnit unit) method inserts the element passed as parameter to method at the tail of this queue(ArrayBlockingQueue) if queue is not full. It will wait till a specified time for space to become available if the queue is full. It returns true when the operation of addition is successful and false if this queue is full and the specified waiting time elapses before space is available. This method is useful when we want to wait for the queue to remove its element and when the queue is not full the element will be added to the queue but we can wait for a specific time and this time can be defined by us.

    Syntax:

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

    Parameter: The method takes three parameters:

    • element (Object)- the element to add in the queue.
    • timeout (long)- how long to wait before giving up, in units of the unit.
    • unit (TimeUnit)- a TimeUnit determining how to interpret the timeout parameter.

    Return Value: The method returns True when adding method is successful and false if the specified waiting time elapses before space is available.

    Exception: The method throws two type of exceptions:

    • InterruptedException – if interrupted while waiting.
    • NullPointerException – if the specified element is null.

    Below programs illustrate the offer(E element, long timeout, TimeUnit unit)method of ArrayBlockingQueue:




    // 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));
        }
    }

    
    

    Output:

    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
    

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ArrayBlockingQueue.html#offer(E)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads