Coordinating Threads

Figure 7. Thread coordination—scheduling and joining

Once we divide a problem into parts, we can schedule several concurrent tasks in a pool of threads and wait for their results to arrive. When these tasks complete, we can proceed as in Figure 7, Thread coordination—scheduling and joining. We don’t want to wait for the threads to die, because these threads may be reused to run several tasks. The result of the scheduled tasks is what we really care about. We can easily achieve this using the Callable interface and the submit or invokeAll methods of ExecutorService. Let’s look at an example.

To find the total size of all ...

Get Programming Concurrency on the JVM now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.