ArrayBlockingQueue take() 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 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.
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:
After addding 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]
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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.