Skip to content

Instantly share code, notes, and snippets.

@ak-git
Created January 12, 2022 15:52
Show Gist options
  • Save ak-git/0e9bcb24dcb6cb2e47469e6458da2263 to your computer and use it in GitHub Desktop.
Save ak-git/0e9bcb24dcb6cb2e47469e6458da2263 to your computer and use it in GitHub Desktop.
Параллельные аккумуляторы https://habr.com/ru/post/599603/
package com.ak.util;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import org.testng.Assert;
import org.testng.annotations.Test;
public class LongAccumulatorTest {
@Test
public void test() {
int n = 50;
LongAccumulator balance = new LongAccumulator(Long::sum, 0L);
ExecutorService executorService = Executors.newFixedThreadPool(n);
Stream.iterate((Runnable) () -> balance.accumulate(1000L), UnaryOperator.identity()).limit(n).forEach(executorService::submit);
executorService.shutdown();
Assert.assertEquals(balance.get(), n * 1000L);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment