From the course: Advanced Java: Threads and Concurrency
Unlock the full course today
Join today to access over 24,600 courses taught by industry experts.
Solution: Asynchronous computations - Java Tutorial
From the course: Advanced Java: Threads and Concurrency
Solution: Asynchronous computations
(lively music) - [Instructor] Now I'll walk you through step by step how to solve the challenge. For the first task, in line number seven, first, I create an executorService. Once again, I'll talk about executors in an upcoming chapter. Then to that, executoeService, I submit the two tasks here in line 10 and 11. This will create two asynchronous tasks that will execute and give us results sometime in future. We can check if the tasks are done and results are available in the futures, which is what I've done in line 13. And if so, call the get method on the futures and obtain and print the results. Finally, I've shut down the executorService. For the second task, I've called the class method supplyAsync to obtain a CompletableFuture in line eight. To this method, I pass a supplier. I've used the lambda expression here, so I've implemented the get method of supplier so that it calculates and returns the sum of even numbers between 0 and 10. Then in line 20, I use thenApply to take in…