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 - Java Tutorial
From the course: Parallel and Concurrent Programming with Java 2
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Welcome to the challenges39s
-
(Locked)
Challenge: Matrix multiply in Java5m 4s
-
(Locked)
Solution: Matrix multiply in Java6m 20s
-
(Locked)
Challenge: Merge sort in Java4m 34s
-
(Locked)
Solution: Merge sort in Java3m 47s
-
(Locked)
Challenge: Download images in Java2m 34s
-
(Locked)
Solution: Download images in Java4m 3s
-
(Locked)
-