package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; class PollLastTimeUnit implements Runnable { String name; BlockingDeque dq; public PollLastTimeUnit(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 = 1; i < 6; i++) { dq.offer(i); try { System.out.println(i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } class PollLastTime implements Runnable { String name; BlockingDeque dq; public PollLastTime(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.pollLast(1, TimeUnit.NANOSECONDS); System.out.println(name + " removes the element " + j); System.out .println("And the remaining size of deque after removing \n the last element (" + j + ") = " + dq.size()); } catch (InterruptedException e) { e.printStackTrace(); } } } public class BdqPollLastTimeUnit { public static void main(String[] args) { BlockingDeque dq = new LinkedBlockingDeque(5); Runnable a = new PollLastTimeUnit("A", dq); Runnable b = new PollLastTime("B", dq); new Thread(a).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(b).start(); } }