Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package com.theboreddev.concurrency;
import com.theboreddev.Processor;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAccumulator;
public class ConcurrentProcessor implements Processor {
private static final int THREADS = 4;
private final LongAccumulator result = new LongAccumulator(Long::sum, 0L);
private final AtomicInteger position = new AtomicInteger(0);
private final ExecutorService executor = Executors.newFixedThreadPool(THREADS);
public Integer process(List<Integer> input) throws InterruptedException {
processArrayConcurrently(input.toArray(new Integer[input.size()]));
return result.intValue();
private void processArrayConcurrently(Integer[] array) throws InterruptedException {
final Runnable runnable = () -> {
while (position.intValue() < array.length) {
IntStream.rangeClosed(1, THREADS)
.forEach(threadNumber -> executor.submit(runnable));
executor.awaitTermination(1, TimeUnit.SECONDS);
private void addElements(Integer[] array) {
int current = position.getAndAdd(2);
final int sum = array[current] + array[current + 1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment