I'm looking at a piece of code now and I'm not sure I understand it correctly. I raised this question to the developer, waiting for his feedback, but I was thinking it would be good to have other feedback.
Here's the piece I'm looking at. We have a list of tables (database objects) and we need to execute the same operation on each of them.
This same operation is defined in Myclass that is implementing Runnable:
final ExecutorService worker = Executors.newFixedThreadPool(8);
...
List.stream()
.parallel()
.filter(table -> table.getRows() > 0 || table.isAutoIncrement())
.forEach(table -> worker.execute(new Myclass())
Here is what I would like to be clarified:
parallel()will split my stream into N sub-streams withN = #cpuson the server - 1. There's no way to control how the stream is split, correct? Does it try to split it equally?- I don't understand the need to use an
ExecutorService. Does it mean only 8 threads will be processing my N sub-streams? - My expectation is to see my 8 threads always working but during my test I saw at the beginning they were running but then the number of threads decreased to 3 for example. Why? Does it mean that at the moment only 3 sub-streams remained to be processed?
The ExecutorService would be used to handle the requests in a background thread. In your example you have a thread pool with 8 threads. So let's imagine the work that needs to be done here takes a significant amount of time. Your code here would work through the stream and hand the work off to the thread pool, and then your main thread would continue running without waiting for the executorservice to finish.
As far as I know, you have no control over how many substreams your stream would be divided into with the parallel() call. Honestly, since you're handing the work off to a threadpool, I'm not sure that that the parallel() call is going to do much for you.