Java ArrayBlockingQueue poll() Method

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:

Parameters:

  • NA
  • timeout - this is the time to wait before giving up(in units of unit)
  • unit - this is the TimeUnit deciding how to represent the timeout parameter

Specified By:

The poll() method of ArrayBlockingQueue class is specified by poll() method in interface Queue<E>.

Return Value:

The poll() method returns this queue's head, or null if this queue is empty or if the assigned waiting time passes before element is available.

Throws:

The poll() method throws:

InterruptedException - if method is disrupted while waiting.

Example 1

Output:

Head of queue is : Reema
Elements left in queue : [Rahul, Rita, Ramesh]

Example 2

Output:

Head of queue : 67
Elements in queue : [109, 76, 876]

Example 3

Output:

Reema gets the highest wages of 12000
Geetanjali gets the second  highest wages of 4500