Java ArrayBlockingQueue take() Method

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:

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 first element of this queue.

Example 1

Output:

Elements are :
67
109
98
123
100
Queue = []

Example 2

Output:

Vinni has passed.
Rima has passed.

Example 3

Output:

Error:(18, 42) java: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown

To resolve this error, either we should add exception to the method signature or should surround the take() method with try and catch.