简体   繁体   中英

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{

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM