Java 8 Streams干扰forEach在一个字段上

5 java concurrency java-8 java-stream

考虑使用java 8流的以下愚蠢程序:

private int biggestInt;

private void run() {
    ExecutorService executor = Executors.newWorkStealingPool();

    List<Callable<Integer>> callables = new ArrayList<>();

    for (int i = 0; i<50; i++) {
        callables.add(randomInt());
    }

    try {
        executor.invokeAll(callables)
            .stream()
            .map(future -> {
                    try {
                        return future.get();
                    } catch (Exception e) {
                        throw new IllegalStateException(e);
                    }
                })
            .forEach(this::compareBiggestInt);
    } catch (InterruptedException e) { /* do nothing */ }
}

private Callable<Integer> randomInt() {
    return () -> {
        Random random = new Random(System.currentTimeMillis());
        return random.nextInt();
    };
}

private void compareBiggestInt(Integer in) {
    if (in > biggestInt)
        biggestInt = in;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,forEach(this :: compareBiggestInt)是并行执行的,因此会在largestInt上引入竞争条件?

如果是这样,我怎样才能避免这种竞争条件?我可以举例如下更改方法吗?

private synchronized void compareBiggestInt(Integer in) {[...]}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏!

Cra*_*tis 2

不,forEach不是并行执行的。这将打破与 相对forEach使用时预期行为的一般契约,并且不受您引入.stream()parallelStream()ExecutorService

实际上invokeAll()返回已完成或超时List的实例。因此,当您与流交互时,并行部分已经完成。Future