Open In App

ArrayBlockingQueue take() method in Java

Last Updated : 29 Jun, 2022
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.

The take() method is used to retrieve and remove the head of this queue. If queue is empty then it will wait until an element becomes available. Syntax:

public E take()throws InterruptedException

Parameters: The method does not take any parameters. 

Return Value: The method returns value at the head of this queue. 

Exception: The method throws InterruptedException if interrupted while waiting. 

Below programs illustrate the take() method of ArrayBlockingQueue: 

Program 1

Java




// Program to demonstrate take() method of ArrayBlockingQueue
 
import java.util.concurrent.ArrayBlockingQueue;
 
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 elements to ArrayBlockingQueue
    queue.add(23);
    queue.add(32);
    queue.add(45);
    queue.add(12);
     
    // Print queue after adding numbers
    System.out.print("After adding numbers Queue: "
                                                +queue);
 
        // Apply take() method
    int head=queue.take();
     
    // Print head of queue using take() method
    System.out.println("Head of queue removed is "
                                                +head);
    System.out.print("After removing head. Queue: ");
    System.out.println(queue);
     
    // Apply take() method
    head = queue.take();
     
    // Print head of queue using take() method
    System.out.println("Head of queue removed is "
                                               + head);
    System.out.print("After removing head. Queue: ");
    System.out.println(queue);
     
}
}


Output:

After adding numbers Queue: [23, 32, 45, 12]Head of queue removed is 23
After removing head. Queue: [32, 45, 12]
Head of queue removed is 32
After removing head. Queue: [45, 12]

Program 2: 

Java




// Program to demonstrate take() method of ArrayBlockingQueue
 
import java.util.concurrent.ArrayBlockingQueue;
 
public class GFG {
 
    // Create a User Object with name
    // and age as the 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)
                        throws InterruptedException
    {
        GFG gfg = new GFG();
        gfg.takeMethodExample();
    }
 
    // Method to give example of take function
    public void takeMethodExample()
                        throws InterruptedException
    {
 
        // define 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
        queue.offer(user1);
        queue.offer(user2);
        queue.offer(user3);
        queue.offer(user4);
        queue.offer(user5);
 
        // find take() of queue
        User head = queue.take();
 
        // print head
        System.out.println("Details of User Removed"
                    +" After Applying take() Method");
        System.out.println("User Name : " + head.name);
        System.out.println("User Age : " + head.age);
 
        // find take() of queue
        head = queue.take();
 
        // print head
        System.out.println("Details of User Removed"+
                      " After Applying take() Method");
        System.out.println("User Name : " + head.name);
        System.out.println("User Age : " + head.age);
    }
}


Output:

Details of User Removed After Applying take() Method
User Name : Aman
User Age : 24
Details of User Removed After Applying take() Method
User Name : Amar
User Age : 23

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



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

Similar Reads