package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; class PollFirstTimeUnit implements Runnable { String name; BlockingDeque dq; public PollFirstTimeUnit(String name, BlockingDeque dq) { this.name = name; this.dq = dq; } public void run() { System.out.println(name + " inserted the element into the deque"); for (int i = 0; i < 5; i++) { dq.offer(i); try { System.out.println(i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } class PollFirstTime implements Runnable { String name; BlockingDeque dq; public PollFirstTime(String name, BlockingDeque dq) { this.name = name; this.dq = dq; } public void run() { try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } try { System.out.println("Therefore deque = " + dq); System.out.println("Size of deque = " + dq.size()); int j = dq.pollFirst(1, TimeUnit.NANOSECONDS); System.out.println(name + " removes the element " + j); System.out .println("And the remaining size of deque after removing \n the first element (" + j + ") = " + dq.size()); } catch (InterruptedException e) { e.printStackTrace(); } } } public class BdqPollFirstTimeUnit { public static void main(String[] args) { BlockingDeque dq = new LinkedBlockingDeque(5); Runnable a = new PollFirstTimeUnit("A", dq); Runnable b = new PollFirstTime("B", dq); new Thread(a).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(b).start(); } }