Is there a way to fasten two threads?

This question arose from the answer to another question , where it was proposed to compare the card and the reduction to calculate the amount at the same time.

There is a complexCalculation(e) in this question, but now I wondered how to parallelize even more by dividing the calculations into two parts, so that complexCalculation(e) = part1(e) * part2(e) . I wonder if it is possible to simultaneously calculate part1 and part2 in the collection (using map() again) and then pin two resultant streams so that the ith element of both streams is combined with the * function, so that the resulting stream is equal to the stream that can be obtained by matching complexCalculation(e) in this collection. In the code, it would look like this:

 Stream map1 = bigCollection.parallelStream().map(e -> part1(e)); Stream map2 = bigCollection.parallelStream().map(e -> part2(e)); // preferably map1 and map2 are computed concurrently... Stream result = map1.zip(map2, (e1, e2) -> e1 * e2); result.equals(bigCollection.map(e -> complexCalculation(e))); //should be true 

So my question is: is there any kind of functionality, like the zip function that I tried to describe here?

+4
source share
2 answers

parallelStream () should be completed in the order presented. This means that you cannot assume that two parallel threads can be archived this way.

Your original bigCollection.map(e -> complexCalculation(e)) will most likely be faster if your collection is actually smaller than the number of processors you have.

+4
source

If you really want to parallelize part1 and part2 (for example, your BigCollection has very few elements, fewer processor cores), you can do the following trick. Suppose you have two methods part1 and part2 in the current class:

 public long part1(Type t) { ... } public long part2(Type t) { ... } 

Create a stream of two functions created from these methods and process it in parallel:

 bigCollection.parallelStream() .map(e -> Stream.<ToLongFunction<Type>>of(this::part1, this::part2) .parallel() .mapToLong(fn -> fn.applyAsLong(e)).reduce(1, (a, b) -> a*b)) .// continue the outer stream operations 

However, this is a very rare case. Since @PeterLawrey noted that your external collection is large enough, you do not need to parallelize part1 and part2 . Instead, you will process individual elements in parallel.

+2
source

All Articles