From the course: Parallel and Concurrent Programming with Java 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: Download images in Java

Solution: Download images in Java - Java Tutorial

From the course: Parallel and Concurrent Programming with Java 2

Start my 1-month free trial

Solution: Download images in Java

(upbeat music) - [Instructor] For our solution to the download images challenge, we considered each of the images that needed to be downloaded as separate tasks, which would each be a call to the download image method. Since we would need to get a result value for the number of bytes from each of these tasks, it seemed like the perfect use case for futures with each task being created as a callable object to be executed by a thread pool. So that's exactly what we did. Lines 59 and 60 in our implementation of the download all method establish a new fixed thread pool with as many workers as there are processors in this system. In our case, that's 24. On line 63, we create a list to hold the futures, and then use a four loop to create a callable object for each of the images that executes the download image method. We decided to create those image request callable objects on line 65 using a lambda expression to keep our code compact, and then we submitted to the thread pool on line 66…

Contents