final Source<Integer, NotUsed> source = // ...
    final MutableInt max = new MutableInt(Integer.MIN_VALUE);
    final Procedure<Integer> f = i -> {
      if (i > max.intValue()) {
        max.setValue(i);
      }
    };
    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
<http://doc.akka.io/docs/akka/2.4.10/scala/stream/stream-flows-and-basics.html#Stream_ordering>
on
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: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
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 akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to