public static Long fanOutFanIn(
      final List<SquareNumberRequest> requests, final Consumer consumer) {

    ExecutorService service = Executors.newFixedThreadPool(requests.size());

    // fanning out
    List<CompletableFuture<Void>> futures =
        requests.stream()
            .map(
                request ->
                    CompletableFuture.runAsync(() -> request.delayedSquaring(consumer), service))
            .toList();

    CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

    return consumer.getSumOfSquaredNumbers().get();
  }
@Test
  void fanOutFanInTest() {
    final List<Long> numbers = Arrays.asList(1L, 3L, 4L, 7L, 8L);

    final List<SquareNumberRequest> requests =
        numbers.stream().map(SquareNumberRequest::new).toList();

    final Consumer consumer = new Consumer(0L);

    final Long sumOfSquaredNumbers = FanOutFanIn.fanOutFanIn(requests, consumer);

    Assertions.assertEquals(139, sumOfSquaredNumbers);
  }