Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 8. Applying Thread Pools > Parallelizing Recursive Algorithms - Pg. 181

8.5. Parallelizing recursive algorithms 181 8.5 Parallelizing recursive algorithms The page rendering examples in Section 6.3 went through a series of refinements in search of exploitable parallelism. The first attempt was entirely sequential; the second used two threads but still performed all the image downloads sequen- tially; the final version treated each image download as a separate task to achieve greater parallelism. Loops whose bodies contain nontrivial computation or per- form potentially blocking I/O are frequently good candidates for parallelization, as long as the iterations are independent. If we have a loop whose iterations are independent and we don't need to wait for all of them to complete before proceeding, we can use an Executor to transform a sequential loop into a parallel one, as shown in processSequentially and processInParallel in Listing 8.10. void processSequentially(List<Element> elements) { for (Element e : elements) process(e); } void processInParallel(Executor exec, List<Element> elements) { for (final Element e : elements)