Java Reference
In-Depth Information
});
synchronized (objectToSync) {
inventoryThread.start();
try {
objectToSync.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread ordersThread = new Thread(() -> {
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();
}
Solution 2
You can control when the main thread continues using a CountDownLatch object.
In the following code, a CountDownLatch with an initial value of 2 is created; then
the two threads for loading the inventory and loading the order information are created
Search WWH ::




Custom Search