ArrayBlockingQueue size() Method in Java

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.

The size() method returns the number of elements that queue contains. Size is an important method to see how many elements the queue contains and how many elements can be added to this queue by calculating the difference between the capacity of queue and size of the queue.
Syntax:

public int size()

Parameters: The method does not take any parameters.
Return Value:The method returns the number of elements in this queue.



Below programs illustrate size() method of ArrayBlockingQueue.
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to Demonstrate the size() 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.add(23);
    queue.add(32);
    queue.add(45);
      
    // Print size of queue after adding numbers
    int size=queue.size();
    System.out.println("After addding numbers"+
                              " Queue size = " +size);
      
    // Add more elements to ArrayBlockingQueue
    queue.add(88);
    queue.add(42);
      
    // Print size of queue after adding numbers
    size=queue.size();
    System.out.println("After addding more numbers"
                               +" Queue size = " +size);
      
}

chevron_right


Output:

After addding numbers Queue size = 3
After addding more numbers Queue size = 5

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to demonstrate size() 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 to store 5 names
    ArrayBlockingQueue<String> names = new 
             ArrayBlockingQueue<String>(capacity);
       
    // Add element to ArrayBlockingQueue
    names.add("Aman");
    names.add("Siddhant");
     
      
    // Print size of queue after adding numbers
    int size = names.size();
    System.out.println("After addding numbers"+
                            " Queue size = "+size);
      
      
    // Add more elements to ArrayBlockingQueue
    names.add("Raunak");
    names.add("Suvo");
      
    // Print size of queue after adding numbers
    size=names.size();
    System.out.println("After addding more numbers"+
                                 " Queue size = "+size);
      
}

chevron_right


Output:

After addding numbers Queue size = 2
After addding more numbers Queue size = 4

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.