package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; class PollTimeUnit implements Runnable { BlockingDeque bdq; public PollTimeUnit(BlockingDeque 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 bdq = new LinkedBlockingDeque(); Runnable a = new PollTimeUnit(bdq); new Thread(a).start(); } }