Member-only story

Scatter & Gather in Java: The Simplest Approach

There are many ways to implement scatter and gather using Java’s concurrency constructs. I’ve tried several approaches and will detail in this article the one that results in the simplest and cleanest code

Basil A.
4 min readAug 23, 2024

Problem Summary

The problem is a very common one, simply we need to run three tasks asynchronously that return a result. We need to wait for the three tasks to complete before using their returned results. Imagine, for example, that each task will connect to a different third-party API endpoint and complete the result.

Asynchronously here means avoiding waiting for each task to complete before starting the next one, this will be much more performant since each of thre three tasks are allowed to execute concurrently without having to depend on each other.

The Simplest Approach

There are many ways to do this in Java, from using raw Threads, to utilizing CyclicBarriers, Phasers, CountDownLatch’es, ExecutorService with Futures and CompletableFutures.

This article won’t walk into each and every possible way to implement it in Java. It will just provide what I’ve found as the…

--

--

Basil A.
Basil A.

Written by Basil A.

A Software Engineer with interests in System Design and Software Engineering done right.

No responses yet