Java examples for Thread:Latch
Control when the main thread continues using a CountDownLatch 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; import java.util.concurrent.CountDownLatch; public class Main { Random random = new Random(); List<String> taskList = new ArrayList<>(); Map<String, Integer> taskMap = new HashMap<>(); Collection<Task> orderList = new ArrayList<>(); CountDownLatch latch = new CountDownLatch(2); private void start() { for (int i = 0; i < 100; i++) { taskList.add("Item #" + i); }/*from w w w . j a v a2 s . co m*/ Thread inventoryThread = new Thread(() -> { taskList.stream().forEach((item) -> { taskMap.put(item, random.nextInt(500)); }); latch.countDown(); }); inventoryThread.start(); Thread ordersThread = new Thread(() -> { for (int i = 0; i < 1000; i++) { Task order = new Task(i); for (int j = 0; j < 10; j++) { String randomItem = taskList.get(random.nextInt(taskList.size())); order.addItem(randomItem, random.nextInt(2) + 1); } orderList.add(order); } latch.countDown(); }); ordersThread.start(); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } orderList.stream().forEach( (order) -> { boolean fulfillable = canFulfill(order); if (fulfillable) { doFulfill(order); System.out.println("Order # " + String.valueOf(order.getTaskId()) + " has been fulfilled"); } else { System.out.println("Order # " + String.valueOf(order.getTaskId()) + " CANNOT be fulfilled"); } }); } private void doFulfill(Task order) { order.getTaskItems().keySet().stream().forEach((item) -> { int quantity = order.getTaskItems().get(item); int currentInventory = taskMap.get(item); taskMap.put(item, currentInventory - quantity); }); } private boolean canFulfill(Task order) { for (String item : order.getTaskItems().keySet()) { int quantity = order.getTaskItems().get(item); int currentInventory = taskMap.get(item); if (quantity > currentInventory) { return false; } } return true; } public static void main(String[] args) { Main recipe = new Main(); recipe.start(); } } class Task { long taskId; private Map<String, Integer> taskItems = new HashMap<>(); Task(long orderId) { this.taskId = orderId; } public void addItem(String itemName, int quantity) { Integer currentQuantity = taskItems.get(itemName); if (currentQuantity == null) { currentQuantity = 0; } taskItems.put(itemName, currentQuantity + quantity); } public Map<String, Integer> getTaskItems() { return taskItems; } public long getTaskId() { return taskId; } }