Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge CompletableFuture | Multithreading Best Practices
Multithreading in Java
course content

Course Content

Multithreading in Java

Multithreading in Java

1. Multithreading Basics
2. Synchronized Collections
3. High-level Synchronization Mechanisms
4. Multithreading Best Practices

bookChallenge CompletableFuture

Task

Your task is to build a system that handles orders and carries out additional calculations like tax and shipping costs. You need to process multiple orders asynchronously, perform the necessary calculations, and display the total amount for each order once all calculations are finished.

Each order has a unique identifier and a price associated with it. (InitMap class)

The ID and their sums are stored in a Map within the InitMap class. The keys of this Map (which are the order ID) are the ones we loop through in the processOrders() method.

Fetching order data is done asynchronously and returns the order amount. This is handled by the OrderService class, specifically the fetchOrderAmount(String orderId) method.

Tax is calculated as 15% of the order amount. This is managed by the CalculationService class, using the calculateTax() method.

Shipping cost is calculated as 10% of the order amount. This is also handled by the CalculationService class, using the calculateShipping() method.

Note

Your main task is to implement the logic step-by-step in the package task class OrderProcessingExample in the processOrders() method.

Implementation Steps

1. Receive the order amount asynchronously using the order service:

  • Create a CompletableFuture object to fetch the order amount asynchronously;
  • Call the fetchOrderAmount() method from OrderService passing the order id.

Note

The fetchOrderAmount() method uses CompletableFuture.supplyAsync() to perform the task asynchronously and returns the order amount, by its id.

2. After fetching the order amount, asynchronously calculate the tax using the calculation service:

  • Use thenCompose() to execute the task sequentially after getting the order amount. Call the calculateTax() method from CalculationService passing the order amount.

Note

The calculateTax() method uses CompletableFuture.supplyAsync() to execute the task asynchronously and returns a tax equal to 15% of the order amount.

3. After receiving the order amount, asynchronously calculate the shipping cost using the calculate service:

  • Use thenCompose() to execute the task sequentially after receiving the order amount. Call the calculateShipping method from CalculationService passing the order amount.

Note

The calculateShipping() method uses CompletableFuture.supplyAsync() to execute the task asynchronously and returns a shipping cost equal to 10% of the order amount.

4. Combine asynchronous tax and shipping cost calculations to get the total incremental cost:

  • Use thenCombine() to combine two CompletableFuture results of tax and shipping to get the total additional cost (tax + shipping cost).

5. Combine the order amount and the total additional cost to get the total order amount:

  • Use thenCombine() to combine the two CompletableFuture results of additional cost and total additional cost and total order amount to get the total order amount.

6. After calculating the total, output it to the console:

  • Use thenAccept() to process and output the result;
  • After completing all calculations, output the order total to the console.

To output the result to the console, you can use this pattern:

If you follow everything correctly, you will get this output to the console:

Once you are sure that everything is working, run the verification tests on the path /src/test/java/OrderProcessingExampleTest.java.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 7
We're sorry to hear that something went wrong. What happened?
some-alt