Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When does reduce() produce different outputs in parallel streaming? #549

Open
limeugene opened this issue Nov 29, 2020 · 6 comments
Open
Labels
🖱️ Java General Questions with regards to Java or its API

Comments

@limeugene
Copy link

Hi, having trouble understanding reduce for parallel streaming.... given that subtraction is non-associative as 3 - (2 - 1) != (3 - 2) - 1, why is reduce() producing the same output every time? When will reduce produce undeterministic results? thanks in advance :)

image

image

@limeugene limeugene added the 🖱️ Java General Questions with regards to Java or its API label Nov 29, 2020
@itsyme
Copy link

itsyme commented Nov 29, 2020

I think its because it takes the same thread to do the same tasks every time

@limeugene
Copy link
Author

@itsyme Hi, would you mind elaborating? Does this mean that I'm not initiating parallel streaming properly in my code? thanks!

@bentanjunrong
Copy link

It has to do with the .parallel() you run. Since your reduce takes in a non associative function, you are not sure of the order of completion of the parallel tasks, so the order of input for the reductions is indeterministic.

@pikasean
Copy link

Have you tried running the function more than twice? If you've only ran it twice, it could be pure coincidence.

@itsyme
Copy link

itsyme commented Nov 29, 2020

It's just my thought but I have seen this in my jshell as well. I think it just happens such that the way it runs is in the same order giving the same result.

@GJ0407790
Copy link

I think can print out the thread name as well to check whether there is only one thread operating or multiple of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖱️ Java General Questions with regards to Java or its API
Projects
None yet
Development

No branches or pull requests

5 participants