Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet193/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   189   190   191   192   193   194   195   196   ...   245
Bog'liq
Java 17 Recipes

 Solution 1
With wait/notify for thread synchronization, threads can be coordinated. In this solution, 
the main thread waits for the objectToSync object until the database-loading thread 
is finished executing. Once the database-loading thread is finished, it notifies the 
objectToSync that whoever is waiting on it can continue executing. The same process 
occurs when loading the orders into our system. The main thread waits on objectToSync 
until the orders-loading thread notifies objectToSync to continue by calling the 
objectToSync.notify() method. After ensuring that both the inventory and the orders 
are loaded, the main thread executes the processOrder() method to process all orders.
public static void main(String[] args) {
Recipe10_7_1 recipe = new Recipe10_7_1();
recipe.start();
}
private final Object objectToSync = new Object();
private void start() {
loadItems();
Thread inventoryThread = new Thread(() -> {
System.out.println("Loading Inventory from Database...");
loadInventory();
synchronized (objectToSync) {
objectToSync.notify();
}
});
synchronized (objectToSync) {
inventoryThread.start();
try {
objectToSync.wait();
} catch (InterruptedException e) {
Chapter 10 ConCurrenCy


376
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();
}
private void processOrders() {
orderList.stream().forEach((order) -> {
boolean fulfillable = canFulfill(order);
if (fulfillable) {
doFulfill (order);
System.out.println("Order # " + String.valueOf(order 
.getOrderId()) + " has been fulfilled");
} else {
System.out.println("Order # "+String.valueOf(order 
.getOrderId())+" CANNOT be fulfilled");
}
});
}
Chapter 10 ConCurrenCy


377
The following is the output.
Loading Inventory from Database...
Loading Orders from XML Web service...
Order # 0 has been fulfilled
Order # 1 has been fulfilled
Order # 2 has been fulfilled
Order # 3 has been fulfilled
Order # 4 has been fulfilled
Order # 5 CANNOT be fulfilled
Order # 6 has been fulfilled
Order # 7 has been fulfilled
Order # 8 has been fulfilled
Order # 9 has been fulfilled
Order # 10 has been fulfilled
Order # 11 has been fulfilled
Order # 12 has been fulfilled
Order # 13 CANNOT be fulfilled
Order # 14 has been fulfilled
Order # 15 CANNOT be fulfilled
Order # 16 CANNOT be fulfilled
Order # 17 has been fulfilled
Order # 18 has been fulfilled
Order # 19 has been fulfilled
Order # 20 has been fulfilled
Order # 21 has been fulfilled
Order # 22 has been fulfilled
...

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   189   190   191   192   193   194   195   196   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling