## Notas
The number of threads spawned by default is one less than the number of cores in CPU.
## Como Functiona
- When a parallel stream is created, it is split into *partitions*, aka smaller substreams
- Work-stealing algorithm: each *Thread* has its own [[deque]] and can steal from another Thread's deque if theirs is empty
- Recursive execution: When a task is encountered, `ForkJoinPool` decides if it should be split or not; the results are later joined
## Best Practices
- Depends on the processes
- If CPU-bound, `numThreads` should be close to `numCores`
- If I/O-bound, can use more threads
- Can use custom `ForkJoinPools` for each type of operation
- Always call `shutdown()` on custom `ForkJoinPool` when finished
## Referencias
- [[Java Flashcards from Interview Feedback]]
- [How Many Threads are Spawned in ParallelStream in Java 8 - StackOverflow](https://stackoverflow.com/questions/30802463/how-many-threads-are-spawned-in-parallelstream-in-java-8)
- [How to Set Java Stream Parallel Stream Thread Count - Java Spring](https://www.javaspring.net/blog/how-to-set-java-stream-parallel-stream-thread-count/)
- [Threads for Parallel Stream - Medium](https://medium.com/@sum98kumar/java-interview-questions-9cb306e392d3)