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

Provide a Parallel Stream utility class #960

Open
Shadows-of-Fire opened this issue May 15, 2024 · 3 comments
Open

Provide a Parallel Stream utility class #960

Shadows-of-Fire opened this issue May 15, 2024 · 3 comments
Labels
enhancement New (or improvement to existing) feature or request

Comments

@Shadows-of-Fire
Copy link
Contributor

Java's parallelStream() method accesses the ForkJoinPool.commonPool when distributing the work, but this thread pool has the incorrect classloader when being used in an environment with Modules. This makes parallelStream() effectively unusuable.

There is a workaround, which is to execute parallelStream() inside of a ForkJoinPool task, which forces it to use the executing pool instead of falling back to the common pool. See https://stackoverflow.com/questions/21163108/custom-thread-pool-in-java-8-parallel-stream

We should provide a native ForkJoinPool with the correct classloader that can be used to execute parallel streams as a utility class, such as this one from FastSuite. This version is an extension of the original from Aequivaleo.

@Shadows-of-Fire Shadows-of-Fire added the enhancement New (or improvement to existing) feature or request label May 15, 2024
@lukebemish
Copy link
Contributor

lukebemish commented May 28, 2024

Given that a parallel stream executed inside a forkjoinpool task works just fine out of the box -- would it be possible to just run mod ctors in a forkjoinpool with the proper classloader and whatnot so that parallel streams works out of the box? I may be misunderstanding something about how parallel mod event dispatch works. The forkjoinpool seems to be set up in ModWorkManager -- would there be a way to make that set the thread context classloader to the relevant mod classloader when executing something?

@Shadows-of-Fire
Copy link
Contributor Author

That would only be relevant for mod construction. If you attempt to use parallelStream() anywhere in your mod's code, you have to ensure that it is running inside a ForkJoinPool task with the correct context classloader, hence the need for the utility.

@lukebemish
Copy link
Contributor

That's a good point, a helper is definitely needed then. If we could get it working out of the box in mod construction too that would awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New (or improvement to existing) feature or request
Projects
None yet
Development

No branches or pull requests

2 participants