Java Thread synchronized on an object
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; public class Main { Random random = new Random(); List<String> itemList = new ArrayList<String>(); Map<String, Integer> inventoryMap = new HashMap<String, Integer>(); Collection<Order> orderList = new ArrayList<Order>(); private final Object objectToSync = new Object(); private void start() { loadItems();/*ww w.j a v a 2s .co m*/ Thread inventoryThread = new Thread(new Runnable() { public void run() { System.out.println("Loading Inventory from Database..."); loadInventory(); synchronized (objectToSync) { objectToSync.notify(); } } }); synchronized (objectToSync) { inventoryThread.start(); try { objectToSync.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } Thread ordersThread = new Thread(new Runnable() { public void run() { System.out.println("Loading Orders from XML Web service..."); loadOrders(); synchronized (objectToSync) { objectToSync.notify(); } } }); synchronized (objectToSync) { ordersThread.start(); try { objectToSync.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } processOrders(); } private void processOrders() { for (Order order : orderList) { boolean fulfillable = canFulfill(order); if (fulfillable) { doFulfill(order); System.out.println("Order # " + String.valueOf(order.getOrderId()) + " has been fulfilled"); } else { System.out.println("Order # " + String.valueOf(order.getOrderId()) + " CANNOT be fulfilled"); } } } private void doFulfill(Order order) { for (String item : order.getOrderedItems().keySet()) { int quantity = order.getOrderedItems().get(item); int currentInventory = inventoryMap.get(item); inventoryMap.put(item, currentInventory - quantity); } } private boolean canFulfill(Order order) { for (String item : order.getOrderedItems().keySet()) { int quantity = order.getOrderedItems().get(item); int currentInventory = inventoryMap.get(item); if (quantity > currentInventory) { return false; } } return true; } private void loadOrders() { for (int i = 0; i < 1000; i++) { Order order = new Order(i); for (int j = 0; j < 10; j++) { String randomItem = itemList.get(random.nextInt(itemList.size())); order.addItem(randomItem, random.nextInt(2) + 1); } orderList.add(order); } } private void loadItems() { for (int i = 0; i < 100; i++) { itemList.add("Item #" + i); } } private void loadInventory() { for (String item : itemList) { inventoryMap.put(item, random.nextInt(500)); } } public static void main(String[] args) { new Main().start(); } } class Order { long orderId; private Map<String, Integer> orderedItems = new HashMap<String, Integer>(); Order(long orderId) { this.orderId = orderId; } public void addItem(String itemName, int quantity) { Integer currentQuantity = orderedItems.get(itemName); if (currentQuantity == null) { currentQuantity = 0; } orderedItems.put(itemName, currentQuantity + quantity); } public Map<String, Integer> getOrderedItems() { return orderedItems; } public long getOrderId() { return orderId; } }