Java ArrayBlockingQueue put() Method7 Jan 2025 | 2 min read The put() method of ArrayBlockingQueue adds the defined element at the tail of this queue. It waits till the space becomes available if queue is full. Syntax:Parameters:e - This is the element to be added. Specified By:The put() method of ArrayBlockingQueue class is specified by put() method in interface BlockingQueue Throws:The put() method throws:
Example 1Output: Elements in queue : [67, 109, 76, 876, 2] Example 2Output: Error:(9, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Error:(10, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Error:(11, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Error:(12, 18) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown Either we should add exception to method signature or we should surround the put() method with try catch, else it will give above described error. Example 3Output: Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ArrayBlockingQueue.checkNotNull(ArrayBlockingQueue.java:150) at java.util.concurrent.ArrayBlockingQueue.put(ArrayBlockingQueue.java:348) at com.javaTpoint.ArrayBlockingQueuePutExample2.main(ArrayBlockingQueuePutExample2.java:13) If the specified element is null, it will give null pointer exception as shown above. Next TopicJava-arrayblockingqueue-removeall-method |
The removeIf() method of Java ArrayBlockingQueue class removes the elements of the ArrayBlockingQueue that satisfies the given predicate filter. Syntax: public boolean removeIf(Predicate<? Super E> filter) Parameters: The parameter filter is a predicate which returns true for the elements to be removed. Specified By: The removeIf() method of ArrayBlockingQueue class is...
3 min read
The size() method of Java ArrayBlockingQueue class returns the total number of elements in the ArrayBlockingQueue. Syntax: public int size() Parameters: NA Specified By: The size() method of ArrayBlockingQueue class is specified by: Size in interface Collection<E>. Size in class AbstractCollection<E> Return Value: The size() method returns the total number of elements in this queue. Example...
3 min read
The remove() method of ArrayBlockingQueue class removes the specified element from the queue, if that element is present in the queue. Syntax: public boolean remove(Object o) Parameters: The parameter 'o' is passed which is removed from the queue, if the element is present. Specified By: The remove () method of ArrayBlockingQueue...
3 min read
The drainTo() method of ArrayBlockingQueue() class removes all the elements present in this queue and adds them to the provided collection. Syntax: public int drainTo(Collection<? super E> c) public int drainTo(Collection<? super E> c, int maxElements) Parameters: c - This is the collection to which elements are transferred. maxElements - This...
2 min read
The forEach() method of Java ArrayBlockingQueue class executes the specified action for each element of Iterable till all the elements are processed or the action throws an exception. Syntax: public void forEach(Consumer<? super E> action) Parameters: action - This is the action to be performed for every element. Specified By: The...
4 min read
ArrayBlockingQueue is a bounded blocking queue which orders the element in FIFO(first-in-first-out). In this queue, new elements are inserted at the tail of this queue and the elements are retrieved from the head of this queue. ArrayBlockingQueue class implements all the optional methods of the Collection...
3 min read
The take() method of Java ArrayBlockingQueue class retrieves and removes the first element of the ArrayBlockingQueue, waiting (if necessary) until an element becomes available. Syntax: public E take() throws InterrupedException Parameters: NA Specified By: The take() method of ArrayBlockingQueue class is specified by: take in interface BlockingQueue<E>. Return Value: The take() method returns the...
3 min read
The remainingCapacity() method of Java ArrayBlockingQueue class returns the capacity of the elements that this queue can accept without blocking. Syntax: public int remainingCapacity() Parameters: NA Specified By: The remainingCapacity () method of ArrayBlockingQueue class is specified by remainingCapacity in interface BlockingQueue. Return Value: The remainingCapacity () method returns the remaining capacity...
3 min read
The Spliterator() method of Java ArrayBlockingQueue class returns a Spliterator over the elements in this ArrayBlockingQueue. Syntax: public Spliterator<E> spliterator() Parameters: NA Specified By: The Spliterator () method of ArrayBlockingQueue class is specified by: Spliterator in interface Collection<E>. Spliterator in interface Iterable<E> Return Value: The Spliterator () method returns a Spliterator over the elements in...
4 min read
The toString() method of Java ArrayBlockingQueue returns a string representation of this collection. Syntax: public String toString() Parameters: NA Overrides toString in class AbstractionCollection<E> Return Value: The toString() method returns a string representation of this collection. Example 1 import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class ArrayBlockingQueueToStringExample1 { public static void main(String[] args) { ...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India