Using Thread.join() to wait for a thread to finish executing - Java Thread

Java examples for Thread:Thread Operation

Description

Using Thread.join() to wait for a thread to finish executing

Demo Code

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> taskList = new ArrayList<>();
  Map<String, Integer> inventoryMap = new HashMap<>();
  Collection<Task> orderList = new ArrayList<>();

  public static void main(String[] args) {
    Main recipe = new Main();
    recipe.start();// w  w  w .  ja va2 s .  com
  }

  private void start() {
    for (int i = 0; i < 100; i++) {
      taskList.add("Item #" + i);
    }

    Thread inventoryThread = new Thread(() -> {
      taskList.stream().forEach((item) -> {
        inventoryMap.put(item, random.nextInt(500));
      });
    });

    inventoryThread.start();
    try {
      inventoryThread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    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);
      }
    });

    ordersThread.start();
    try {
      ordersThread.join();
    } 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 = inventoryMap.get(item);
      inventoryMap.put(item, currentInventory - quantity);
    });
  }

  private boolean canFulfill(Task order) {
    for (String item : order.getTaskItems().keySet()) {
      int quantity = order.getTaskItems().get(item);
      int currentInventory = inventoryMap.get(item);
      if (quantity > currentInventory) {
        return false;
      }
    }
    return true;
  }
}

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;
  }
}

Result


Related Tutorials