4

Reading Java 8's streaming API for parallelism: https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

It's not clear how to tune the number of threads to use when using this streaming API's parallelism?

Plan to run this on a very specific type of machine and a consistent data types, so I was thinking I can benchmark it over a set of different settings then use the best number of threads.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
MikeW
  • 151
  • 1
  • 2
  • 5

1 Answers1

4

The stream API does not support any tuning parameters. The library is designed to work as effectively as possible regardless of the particular machine's characteristics.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Then using traditional threads would be appropriate? – MikeW Jun 16 '15 at 21:05
  • 1
    I didn't say that. I would trust the stream API to be better implemented than anything you wrote from scratch, even if you had information about the machine. – Louis Wasserman Jun 16 '15 at 21:06
  • 3
    To put it another way, there are no tuning parameters because it knows better than you. – Louis Wasserman Jun 16 '15 at 21:07
  • Well, there is that system property I guess. – Hakanai Jun 25 '15 at 03:42
  • 1
    I wouldn't say that it ALWAYS knows better than you. Tuning would have been useful when the processing is IO-bound. Consider something like map(StockUtil::getPrice()) that goes to a web service to get the price for the 1000 ticker symbols in the list. – Bogdan Calmac Aug 13 '15 at 14:20
  • @Louis I strongly disagree. I'm trying to run slow network requests in parallel on a backend server. Probably I need to use the undocumented [ForkJoinPool-Solution](https://stackoverflow.com/a/22269778/2448440), which is IMO worse than adding an optional parameter for a max number of threads. -.- – Qw3ry Sep 21 '17 at 10:59
  • @Qw3ry: streams are not intended for that use case. Streams are intended for local, computational work. – Louis Wasserman Sep 21 '17 at 18:21
  • 1
    @LouisWasserman is this intention documented somewhere? Because for me, Streams feel like a (general purpose) language construct (it is `java.*lang*.stream.Stream` after all). In some cases I might not even know (or care) how my data is processed (in my concrete case I'm looking into caching right now - that will actually change the _how is my data processed_ during runtime). – Qw3ry Sep 22 '17 at 06:33