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 put (E e) method inserts element passed as parameter to method at the tail of this queue(ArrayBlockingQueue) if queue is not full. If the queue is full then it will wait for space to become available.
public void put(E e) throws InterruptedException
e – the element to add in queue.
InterruptedException – if interrupted while waiting.
NullPointerException – if the specified element is null.
Below programs illustrate put(E e) method of ArrayBlockingQueue.
Output : queue contains [223, 546, 986, 357, 964]
Output : queue contains [StarWars, SuperMan, Flash, BatMan, Avengers] queue contains [Flash, BatMan, Avengers, CaptainAmerica, Thor]
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.