Coordinating Threads

images/scalabilityAndTreadSafety/threadjoin.png
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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.