Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what application niche is parallelStream from Java useful?

I'm trying to identify a class of Java applications that could benefit from use of parallelStream API introduced in Java 8.

I'm aware of the numerous caveats of the API described in other SO posts :

  • Shared fork/join pool, with non trivial starting time, and some potential issues with contention in the pool
  • Uncontrolled use of system resources in a way that makes using this sort of code on a server (that already has a multi-task policy) might actually be a bad idea
  • ... there are other criticisms mostly related to performance

Still, the API offers to make use of modern multicore machines with code that is not very intrusive provided Stream API is already used, so no hassle multi-threading at low development cost. I would therefore still like to think it can be useful in some scenarios.

I'm thinking the application context thus has to be something like :

  1. my application is currently sequential
  2. there is a response time issue, in terms of wall clock time, e.g. the user clicked a GUI button and is waiting for reply
  3. the application is running on client machines, where most of the time we can expect to have some available CPU cores, not on a server where resources are already contended
  4. my development team does not have the manpower/skills to develop their own task allocation/threading mechanism, so they would not go for parallelism unless they can do it easily using this API

I searched on github, but it's quite hard to find relevant examples of parallelStream usage that are not exercises or textbook examples (I'd welcome links to some usage in midsize+ projects of the API).

So which kind of applications were the Java language developers targetting with this API ?

Would you agree with the above requirements on the application context for the API to be useful ?

like image 208
Yann TM Avatar asked Jul 03 '20 13:07

Yann TM


People also ask

What is the use of parallelstream in Java?

The parallelStream () is a method from the Collection interface. It returns a possible parallel stream with the collection as the source. In the code below, again a parallel stream is used but here a List is used to read from the text file that is why we need the parallelStream () method.

How does the parallelstream () method of the collection interface work?

The parallelStream () method of the Collection interface returns a possible parallel stream with the collection as the source. Let us explain the working with the help of an example. In the code given below, we are again using parallel streams but here we are using a List to read from the text file.

What is the advantage of parallel method on streams only?

As you can see parallel method can be applied with streams only. parallel () applied on Prime number. As you can see in the above code, even the parallel method on streams also performs prime number, even number, odd number, etc., logic. Given below are the advantages and applications: Improves CPU utilization more efficiently than normal filters.

Is a stream executed sequentially or in parallel?

A stream is executed sequentially or in parallel depending on the execution mode of the stream on which the terminal operation is initiated. The Stream API makes it possible to execute a sequential stream in parallel without rewriting the code.


3 Answers

This looks like a nice explanation of cases of where and why. https://computing.llnl.gov/tutorials/parallel_comp/#WhyUse I personally see no interesting cases in user centered web applications.

The fork/join Framework is a really cool low level api. Many other higher level frameworks use it under the hood very successfuly. I've used it for test data generation. Cache bootstraping. Data processing etc... In many cases you get a really good boost of performance in others its just unnecessary overhead.

like image 143
Armando Ballaci Avatar answered Oct 25 '22 12:10

Armando Ballaci


A similar question is asked in Should I always use a parallel stream when possible? Note the second answer is given by Brian Goetz, a Java language architect at Oracle who was involved in the design of the Stream API, so his answer may be considered authoritative.

Top answers are quick to point out that parallel streams include additional overhead necessary for coordination and thus will only increase performance in scenarios where the amount of individual processing per stream is significant enough that the gain from parallel processing overcomes that initial overhead.

Unsurprisingly, as with any question of performance, the advice is to measure rather than guess. Start with a sequential stream, and if you have a large number of elements each requiring complex computation, measure the performance difference of switching to parallel streams.

Additional guidelines, such as those listed in the OP, may be helpful; but people are notoriously bad at identifying performance bottlenecks, so any guidelines are likely to fail eventually in the face of actual measurements.

like image 29
jaco0646 Avatar answered Oct 25 '22 10:10

jaco0646


the application is running on client machines, where most of the time we can expect to have some available CPU cores, not on a server where resources are already contended

This prediction does not have any foundation. Both on desktop and server machines, there could be only your application running or there could be 1,000s of applications running.

There is no "application niche" in which parallel streams are useful. You should use them only if you make sure, either via quantitative or qualitative measuring, that performance is improved, and their disadvantages do not matter too much.

They are easy only if you understand the concepts beneath. They can be applied only to a specific subset of problems.

I would consider using them only if:

  • all stream operations are pure functions and therefore do not require synchronization
  • the performance is not critical, however a boost would be great (so contention of the shared pool can be tolerated)
like image 24
Atom 12 Avatar answered Oct 25 '22 12:10

Atom 12