Java Reference
In-Depth Information
Solution 3
By using Thread.join() , you can wait for a thread to finish executing. The follow-
ing example has a thread for loading the inventory and another thread for loading the
orders. Once each thread is started, a call to inventoryThread.join() will
make the main thread wait for the inventoryThread to finish executing before
continuing.
private void start() {
loadItems();
Thread inventoryThread = new Thread(() -> {
System.out.println("Loading Inventory from
Database...");
loadInventory();
});
inventoryThread.start();
try {
inventoryThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread ordersThread = new Thread(() -> {
System.out.println("Loading Orders from XML Web
service...");
loadOrders();
});
ordersThread.start();
try {
ordersThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
processOrders();
}
Search WWH ::




Custom Search