TheFunk
TheFunk

Reputation: 1108

Java 8 stream within stream parallelism

I have a fairly complicated process that requires several levels of nested for loops.

Actions are only performed for one specific set of conditions. In other words:

for(){ 
    if(){ 
        for(){ 
            if(){
                //Something happens RIGHT HERE
            }
            //And maybe here
        }
    }
}

No else statements, just one solid code path iterating through a bunch of different types of objects.

My question is, if I were to replace this logic with streams (considering how many operations the CPU has to perform to complete this cycle, I think paralleling the process is the way to go) and I have a stream within a stream within a stream (BWOOOMMMPPPPPPP INCEPTION NOISE) and I parallelstream() the top level, will the streams beneath that top level still run in sequence, but just within their own respective threads?

    toplevelItems.parallelstream().forEachOrdered{
       //Does this stuff run in series within as many threads as there are toplevelItems
       otherObjects.stream().forEach{
           //or Naw?
           stillOtherObjects.stream().forEach{

Upvotes: 4

Views: 265

Answers (1)

mvd
mvd

Reputation: 2720

Anything below the top parallelStream() is going to be done in a serial fashion. I assume you are working with something like a List<List<>>, so you will be creating a new stream below the top level which has no connection to the top one.

Upvotes: 1

Related Questions