Technically I think the solution holds water, there are appropriate
barriers in place. (multi-materialization aside)

However, as mentioned, fold() would be a much cleaner, safer and
maintainable solution.

-- 
Cheers,
√

On Sep 21, 2016 07:05, "Roland Kuhn" <goo...@rkuhn.info> wrote:

> Technically I'm not sure this is correct: since f is only called from
> within a single operator (and assuming only a single materialization) the
> shown code should work just fine. Of course it is necessary to know what
> you're doing when coding like this, and as Konrad says there are better
> alternatives available, but guaranteeing ordering for example for db writes
> would certainly be desirable.
>
> Regards, Roland
>
> Sent from my iPhone
>
> On 21 Sep 2016, at 01:33, Konrad Malawski <konrad.malaw...@lightbend.com>
> wrote:
>
>
>     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.
>
> --
> >>>>>>>>>> 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.
>

-- 
>>>>>>>>>>      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