Java LinkedBlockingDeque pollFirst() Method

The pollFirst() method of LinkedBlockingDeque class retrieves the first element of this deque, and also removes it. The method returns null if this deque is empty.

The pollFirst(long timeout, TimeUnit unit) method of LinkedBlockingDeque class retrieves the first element of this deque, awaiting till the specified time is essential for an element to be available.

Syntax:

Parameters:

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

Specified By:

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

  1. pollFirst() method in interface Deque<E>.
  2. pollFirst() method in interface BlockingDeque<E>.

Return Value:

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

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

Throws:

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

Example 1

Test it Now

Output:

First element of Deque is: Rahim

Deque after using pollFirst() method : [Sagar, Tarun, Vibhav, Zarine, Ashwat]

Example 2

Test it Now

Output:

First element of Deque is: 15246
Deque after using pollFirst() method : [24556, 32784, 41275, 59322, 68844]





Latest Courses