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 |
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 removeAll() method of Java ArrayBlockingQueue class removes the elements of the ArrayBlockingQueue which are contained in the specified collection. Syntax: public boolean removeAll(Collection<?> c) Parameters: Here, the parameter c is the collection containing elements to be removed from ArrayBlockingQueue. Specified By: The removeAll () method of ArrayBlockingQueue...
4 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 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
The poll() method of ArrayBlockingQueue class retrieves the head of this queue awaiting till the specified time if essential for an element to be available, and also removes it. The method returns null if this queue is empty. Syntax: 1.public E poll() 2.public E poll(long timeout,TimeUnit unit)throws InterruptedException Parameters: NA timeout...
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 clear() method of ArrayBlockingQueue class automatically withdraws all the elements from this priority queue. After calling this method, the queue will be empty. Syntax: public void clear() Parameters: NA Specified By: The clear() method of class ArrayBlockingQueue is specified by clear() method in interface Collection. Override: The clear() method of class ArrayBlockingQueue overrides...
2 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 retainAll() method of Java ArrayBlockingQueue class retains only the elements of the ArrayBlockingQueue that are contained in the specified collection and removes rest of the elements. Syntax: public boolean retainAll(Collection<?> c) Parameters: Here, the parameter c is the collection containing elements to be retained in the ArrayBlockingQueue collection. Specified...
3 min read
The iterator() method of ArrayBlockingQueue class returns an iterator across the elements of this queue in a sequential manner. The elements returned will follow order from first (head) to last (tail). Syntax: public Iterator<E> iterator() Parameters: NA Specified By: The iterator() method of ArrayBlockingQueue class is specified by: iterator() method in interface Collection<E>. iterator()...
3 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