Created
January 12, 2022 15:52
-
-
Save ak-git/0e9bcb24dcb6cb2e47469e6458da2263 to your computer and use it in GitHub Desktop.
Параллельные аккумуляторы https://habr.com/ru/post/599603/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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