Java Thread synchronized on LinkedHashMap
import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; public class Main { Map<String, Integer> inventoryMap = new LinkedHashMap<String, Integer>(); List<CustomerOrder> customerOrders = new ArrayList<CustomerOrder>(); Random random = new Random(); Set<Thread> orderingThreads = new HashSet<Thread>(); public static void main(String[] args) throws InterruptedException { new Main().start(); }//from www. j a va2 s. c om private void start() throws InterruptedException { for (int i = 0; i < 100; i++) { inventoryMap.put("Book #" + i, 1000); } for (int i = 0; i < 20; i++) { createOrderingThread(); } Thread.sleep(100); checkInventoryLevels(); Thread.sleep(100); for (Thread thread : orderingThreads) { thread.interrupt(); } Thread.sleep(1000); checkInventoryLevels(); displayOrders(); } private void displayOrders() { synchronized (inventoryMap) { for (CustomerOrder order : customerOrders) { System.out.println(order.getQuantityOrdered() + " " + order.getItemOrdered() + " for " + order.getCustomerName()); } } } private void createOrderingThread() { Thread orderingThread = new Thread(new Runnable() { public void run() { while (!Thread.interrupted()) { createRandomOrder(); } } }); orderingThread.start(); orderingThreads.add(orderingThread); } private void createRandomOrder() { String itemOrdered = "Book #" + random.nextInt(100); int quantityOrdered = random.nextInt(2) + 1; String customerName = "Customer :" + System.currentTimeMillis(); fulfillOrder(itemOrdered, quantityOrdered, customerName); } private boolean fulfillOrder(String itemOrdered, int quantityOrdered, String customerName) { synchronized (inventoryMap) { int currentInventory = inventoryMap.get(itemOrdered); if (currentInventory < quantityOrdered) { System.out.println("Couldn't fulfill order for " + customerName + " not enough " + itemOrdered + " (" + quantityOrdered + ")"); return false; // sold out } inventoryMap.put(itemOrdered, currentInventory - quantityOrdered); CustomerOrder order = new CustomerOrder(itemOrdered, quantityOrdered, customerName); customerOrders.add(order); System.out.println("Order done for " + customerName + " of " + itemOrdered + " (" + quantityOrdered + ")"); return true; } } private void checkInventoryLevels() { synchronized (inventoryMap) { for (Map.Entry<String, Integer> inventoryEntry : inventoryMap.entrySet()) { System.out.println("Inventory Level :" + inventoryEntry.getKey() + " " + inventoryEntry.getValue()); } } } } class CustomerOrder { String itemOrdered; int quantityOrdered; String customerName; CustomerOrder(String itemOrdered, int quantityOrdered, String customerName) { this.itemOrdered = itemOrdered; this.quantityOrdered = quantityOrdered; this.customerName = customerName; } public String getItemOrdered() { return itemOrdered; } public int getQuantityOrdered() { return quantityOrdered; } public String getCustomerName() { return customerName; } }