Java ConcurrentLinkedDeque solve producer and consumer problem

Description

Java ConcurrentLinkedDeque solve producer and consumer problem

import java.util.concurrent.ConcurrentLinkedDeque;

public class Main {
   /*from  w  w  w . j av a 2 s.  c  om*/
   public static void main(String[] args) {
      ConcurrentLinkedDeque<Item> deque = new ConcurrentLinkedDeque<>();
      Thread producerThread = new Thread(new ItemProducer(deque));
      Thread consumerThread = new Thread(new ItemConsumer(deque));
      producerThread.start();
      consumerThread.start();
   }

}
class Item {
   private String description;
   private int itemId;

   public String getDescription() {
      return description;
   }

   public int getItemId() {
      return itemId;
   }

   public Item() {
      this.description = "Default Item";
      this.itemId = 0;
   }

   public Item(String description, int itemId) {
      this.description = description;
      this.itemId = itemId;
   }
}
class ItemProducer implements Runnable {
   private ConcurrentLinkedDeque<Item> deque;
   public ItemProducer( ConcurrentLinkedDeque<Item> deque) {
      this.deque= deque;
   }
   @Override
   public void run() {
      String itemName = "";
      int itemId = 0;
      try {
         for (int x = 1; x < 8; x++) {
            itemName = "Item" + x;
            itemId = x;
            deque.add(new Item(itemName, itemId));
            System.out.println("New Item Added:" + itemName + " " + itemId);
            Thread.currentThread().sleep(250);
         }
      } catch (InterruptedException ex) {
         ex.printStackTrace();
      }
   }
}
class ItemConsumer implements Runnable {
   private ConcurrentLinkedDeque<Item> deque;
   public ItemConsumer( ConcurrentLinkedDeque<Item> deque) {
      this.deque= deque;
   }

   @Override
   public void run() {
      try {
         Thread.currentThread().sleep(1000);
      } catch (InterruptedException ex) {
         ex.printStackTrace();
      }
      Item item;
      while ((item = deque.pollFirst()) != null) {
         if (item == null) {
            // wait
         } else {
            generateOrder(item);
         }
      }
   }

   private void generateOrder(Item item) {
      System.out.println("Part Order");
      System.out.println("Item description: " + item.getDescription());
      System.out.println("Item ID # " + item.getItemId());
      System.out.println();
      try {
         Thread.currentThread().sleep(1000);
      } catch (InterruptedException ex) {
         ex.printStackTrace();
      }
   }
}



PreviousNext

Related