Java LinkedBlockingDeque poll() Method

The poll() method of LinkedBlockingDeque class retrieves the head of the queue corresponding to this deque, and also removes it. This method returns null if this deque is empty.

The poll(long timeout, TimeUnit unit) method of LinkedBlockingDequeclass retrieves the head of the queue corresponding to this deque, awaiting till the specified time is essential for an element to be available.

Syntax:

Parameters:

  1. NA
  2. timeout - It is the time to wait before giving up(in units of unit)
  3. unit - It is the TimeUnit deciding how to represent the timeout parameter

Specified By:

The poll() method of LinkedBlockingDeque class is specified by:

  1. poll() method in interface Queue<E>.
  2. poll() method in interface Deque<E>.
  3. poll() method in interface BlockingDeque<E>.

Return Value:

The poll() method returns this deque's head, and if this deque is empty returns null.

The poll(long timeout,TimeUnit unit) method returns this deque's head, or null if the assigned waiting time passes before element is available.

Throws:

The poll() method throws InterruptedException if method is disrupted while waiting.

Example 1

Test it Now

Output:

Deque before using poll() method : [Rahul, Sagar, Tarun, Vaibhav]

Head of Deque is: Rahul

Deque after using poll() method : [Sagar, Tarun, Vaibhav]

Example 2

Test it Now

Output:

Deque before using poll() method : [15023001, 15023002, 15023003, 15023005, 15023007, 15023011]

Head of Deque is: 15023001

Deque after using poll() method : [15023002, 15023003, 15023005, 15023007, 15023011]

Example 3

Test it Now

Output:

First element of Deque is: Dwayne
Deque after using poll() method : [Adam, Mark, Robret, Winston, Peter]





Latest Courses