From the course: Advanced Java: Threads and Concurrency

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Solution: Number magic and voting simulator

Solution: Number magic and voting simulator - Java Tutorial

From the course: Advanced Java: Threads and Concurrency

Solution: Number magic and voting simulator

(upbeat electronic music) - Now I'll walk you through step by step how I solve this challenge. To solve task one, I've written this code in the NumberMagic class. In line 25, I create and execute a service which has an internal thread pool of three threads. You can have a thread pool with any number of threads. Then from line 27 to 43, I have code to create the three tasks. That is, find the square route, the factorial, and represent the given number in binary format. You can see that I've created the tasks as callables as I need to return their results. In line 45, I add the three callable tasks into an ArrayList, and then in line 50, I call the InvokeAll method of the executor service as I want to execute all three tasks and get results from all three of them. And you can see that I passed my list of tasks to the method. In line 53, I have a for loop where I look through the return list of futures and call the get method of each to retrieve the results. Finally, I shut down the…

Contents