1

I saw this code somewhere using stream().map().reduce().

Does this map() function really works parallel? If Yes, then how many maximum number of threads it can initiate for map() function?

What if I use parallelStream() instead of just stream() for the below particular use-case.

Can anyone give me good example of where to NOT use parallelStream()

Below code is just to extract tName from tCode and returns comma separated String.

String ts = atList.stream().map(tcode -> {
    return CacheUtil.getTCache().getTInfo(tCode).getTName();
}).reduce((tName1, tName2) -> {
    return tName1 + ", " + tName2;
}).get();
greg-449
  • 109,219
  • 232
  • 102
  • 145
Jigar Naik
  • 1,946
  • 5
  • 29
  • 60

2 Answers2

5

this stream().map().reduce() is not parallel, thus a single thread acts on the stream.

you have to add parallel or in other cases parallelStream (depends on the API, but it's the same thing). Using parallel by default you will get number of available processors - 1; but the main thread is used too in the ForkJoinPool#commonPool; thus there will be usually 2, 4, 8 threads etc. To check how many you will get, use:

Runtime.getRuntime().availableProcessors()

You can use a custom pool and get as many threads as you want, as shown here.

Also notice that the entire pipeline is run in parallel, not just the map operation.

There isn't a golden law about when to use and when not to use parallel streams, the best way is to measure. But there are obvious choices, like a stream of 10 elements - this is way too little to have any real benefit from parallelization.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • 2
    *"You can use a custom pool and get as many threads as you want ..."* - but if the computation is CPU bound, then creating more threads than available cores is not going to speed things up. – Stephen C Jun 28 '17 at 07:27
  • 1
    @StephenC yes... I think that is also mentioned in the linked question – Eugene Jun 28 '17 at 07:56
0

All parallel streams use common fork-join thread pool and if you submit a long-running task, you effectively block all threads in the pool. Consequently you block all other tasks that are using parallel streams.

There are only two options how to make sure that such thing will never happen. The first is to ensure that all tasks submitted to the common fork-join pool will not get stuck and will finish in a reasonable time. But it's easier said than done, especially in complex applications. The other option is to not use parallel streams and wait until Oracle allows us to specify the thread pool to be used for parallel streams.

Use case Lets say you have a collection (List) which gets loaded with values at the start of application and no new value is added to it at any later point. In above​ scenario you can use parallel stream without any concerns.

Don't worry stream is efficient and safe.

Govind J
  • 19
  • 2