final Source<Integer, NotUsed> source = // ...
    final MutableInt max = new MutableInt(Integer.MIN_VALUE);
    final Procedure<Integer> f = i -> {
      if (i > max.intValue()) {
    final CompletionStage<Integer> result = source
        .runForeach(f, materializer)
        .thenApply(__ -> max.intValue());

Is the above code correct in the sense that it always commutes the maximum
(or MIN_VALUE in case the stream is empty)?

According to Akka Stream documentation
Stream Ordering, there is a Java happens-before relation between
invocations of f. Do I still need to use AtomicInteger so that the effects
are seen by all threads?

You're side-effecting outside the stream, so nothing is guaranteed.

Yes, it would have to be an atomic integer.

Instead consider using a better operator for this kind of thing than
foreach - like fold (runFold) which is enough for such operation you're
doing here.

Happy hakking.

-- konrad

>>>>>>>>>>      Read the docs:
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>>      Search the archives:
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
To post to this group, send email to
Visit this group at
For more options, visit

Reply via email to