Java Reference
In-Depth Information
and started. As each of the two threads finish executing, they call the Coun-
tDownLatch 's countDown() method, which decrements the latch's value by one.
The main thread waits until the CountDownLatch reaches 0 , at which point it re-
sumes execution.
CountDownLatch latch = new CountDownLatch(2);
private void start() {
loadItems();
Thread inventoryThread = new Thread(() -> {
System.out.println("Loading Inventory from
Database...");
loadInventory();
latch.countDown();
});
inventoryThread.start();
Thread ordersThread = new Thread(() -> {
System.out.println("Loading Orders from XML Web
service...");
loadOrders();
latch.countDown();
});
ordersThread.start();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
processOrders();
}
Search WWH ::




Custom Search