Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
Solution 3
Thread.join() allows you to wait for a thread to finish executing. The following 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() make the main thread wait for the inventoryThread to finish executing before continuing. public static void main(String[] args) { Recipe10_7_3 recipe = new Recipe10_7_3(); recipe.start(); } private void start() { loadItems(); Thread inventoryThread = new Thread(() -> { System.out.println("Loading Inventory from Database..."); loadInventory(); }); inventoryThread.start(); try { inventoryThread.join(); } catch (InterruptedException e) { Chapter 10 ConCurrenCy 380 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(); } 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 has been 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 has been fulfilled ... Chapter 10 ConCurrenCy |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling