This method finds and deletes the head element from the deque, to retrieve as well as remove an element this method waits (if necessary) until the waiting time is declared for becoming an element to be available.
poll(timeout, unit) method of BlockingDeque Interface in Java.
In this section we will discuss how can poll(timeout, unit) method be implemented in BlockingDeque interface in java.
Syntax
E poll(long timeout, TimeUnit unit)
This method finds and deletes the head element from the deque, to retrieve as well as remove an element this method waits (if necessary) until the waiting time is declared for becoming an element to be available. It returns the head element, or 'null' if the specified waiting time lapses before an element is available.
Parameter description
E : It is the type of parameter in which the element has to be returned.
timeout : It is the waiting time that how long to wait before giving up, in units of unit.
unit : It is the unit of time which determines how to interpret the timeout parameter. Written as 'TimeUnit.Seconds' (or, microsecond, milliseconds, nanosecond, etc).
Example of poll(timeout, unit) method
In this example we will show you how does poll(timeout, unit) method work in BlockingDeque interface. This example will help you to understand how can you retrieve and remove the element with specified waiting time from the head position of the underlying deque.
Example :
package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; class PollTimeUnit implements Runnable { BlockingDeque<Integer> bdq; public PollTimeUnit(BlockingDeque<Integer> bdq) { this.bdq = bdq; } public void run() { int i; Integer it, p; for (i = 1; i < 5; i++) { bdq.add(i); } try { System.out.println("Elements of deque are : " + bdq); System.out.println("Size of deque = "+bdq.size()); it=bdq.peek(); System.out.println("The head element of the underlying deque is : "+ it); Thread.sleep(300); System.out.println("Therefore the currently removed element = "+ bdq.poll(2, TimeUnit.SECONDS)); System.out.println("After removing the head element from \n deque the remaining element of deque = "+ bdq+" and size of deque= "+bdq.size()); System.out.println("Now remove all the elements of this deque"); bdq.clear(); System.out.println("Size of deque = "+bdq.size()); it=bdq.peek(); System.out.println("The head element of the underlying deque is : "+ it); if(it==null){ System.out.println("Deque contains no element"); } else{ p = bdq.poll(1, TimeUnit.SECONDS); System.out.println("The currently removed element is = " + p); } } catch (InterruptedException e) { e.getMessage(); } } } public class BdqPollTimeUnit { public static void main(String args[]) { BlockingDeque<Integer> bdq = new LinkedBlockingDeque<Integer>(); Runnable a = new PollTimeUnit(bdq); new Thread(a).start(); } }
Output :
Elements of deque are : [1, 2, 3, 4] Size of deque = 4 The head element of the underlying deque is : 1 Therefore the currently removed element = 1 After removing the head element from deque the remaining element of deque = [2, 3, 4] and size of deque= 3 Now remove all the elements of this deque Size of deque = 0 The head element of the underlying deque is : null Deque contains no element |
[ 0 ] Comments