package devmanuals.com; import java.util.concurrent.BlockingDeque; import java.util.concurrent.LinkedBlockingDeque; class RemoveLastOccurrence implements Runnable { BlockingDeque bdq; String name; public RemoveLastOccurrence(String name, BlockingDeque bdq) { this.bdq = bdq; this.name = name; } public void run() { int i; System.out.println(name + " inserted the element into the deque"); for (i = 0; i < 5; i++) { bdq.add(i); System.out.println(i); try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } } bdq.add(2); System.out.println(name + " adds the new element into the deque then new deque = " + bdq); System.out.println("Size of deque = " + bdq.size()); bdq.removeLastOccurrence(2); System.out .println("Remaining element of deque after removing the " +"\n first occurrence of '2' = "+ bdq); } } class RemoveLastOccurrence1 implements Runnable { String name; BlockingDeque bdq; public RemoveLastOccurrence1(String name, BlockingDeque bdq) { this.bdq = bdq; this.name = name; } public void run() { try { bdq.remove(); Thread.sleep(1500); System.out.println(name + " deleted the first element then remaining deque = " + bdq); System.out.println("Size of deque = " + bdq.size()); } catch (InterruptedException e) { e.printStackTrace(); } } } public class BdqRemoveLastOccurrence { public static void main(String args[]) { BlockingDeque bdq = new LinkedBlockingDeque(5); Runnable a = new RemoveLastOccurrence("A", bdq); new Thread(a).start(); try { Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } Runnable b = new RemoveLastOccurrence1("B", bdq); new Thread(b).start(); } }