package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; class OfferFirst implements Runnable { BlockingDeque bdq; public OfferFirst(BlockingDeque bdq) { this.bdq = bdq; } public void run() { System.out.println("Elements of deque1 are : "); int i; boolean bol = false; for (i = 0; i < 5; i++) bdq.offer(i); System.out.println(bdq); System.out.println("Size of deque = " + bdq.size()); try { Thread.sleep(1000); bol = bdq.offerFirst(5); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("The element is added into the deque1 : " + bol); System.out.println("Deque after adding element : " + bdq); System.out .println("Size of deque after adding element = " + bdq.size()); } } class OfferFirst1 implements Runnable { BlockingDeque bdq1; public OfferFirst1(BlockingDeque bdq1) { this.bdq1 = bdq1; } public void run() { System.out.println("Elements of deque2 are : "); int i; boolean bol = false; for (i = 0; i < 5; i++) bdq1.offer(i); System.out.println(bdq1); System.out.println("Size of deque = " + bdq1.size()); try { Thread.sleep(1500); bol = bdq1.offerFirst(5); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("The element is added into the deque2 : " + bol); if (bol == true) { System.out.println("Deque2 after adding element : " + bdq1); System.out.println("Size of deque after adding element = " + bdq1.size()); } else { System.out.println("The element is not added into the deque2."); System.out .println("Elements and Size of deque2 is still \n remains same i.e. size= " + bdq1.size() + " and elements= " + bdq1); } } } public class BdqOfferFirst { public static void main(String[] args) { BlockingDeque bdq = new LinkedBlockingDeque(); BlockingDeque bdq1 = new LinkedBlockingDeque(4); Runnable a = new OfferFirst(bdq); Runnable b = new OfferFirst1(bdq1); new Thread(a).start(); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(b).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }