How it works...

Both types of stream use threads in java.util.concurrent.ForkJoinPool to execute all aggregate methods. The only difference is that the sequential stream uses only one thread, while the parallel one uses the default number of threads offered by the CPU of the machine. To enable parallelStream() is much easier than managing the threads that it uses. If the default number of threads degrades the parallel processing mechanism, ForkJoinPool can be customized by setting the number of allowable threads, which must not be more than the number of core processors present in the machine. But the stream pipeline must be wrapped in a java.util.concurrent.Callable object in order to be executed by the thread pool.

Since parallelStream() uses concurrent threads, it can help lower the processing time of some generic transactions only. But it has several drawbacks on synchronized transactions that involve saving mutable objects, connecting to remote services, processing tightly-coupled transactions, and computing highly complicated mathematical and statistical formulas. It also has some bad effects on APIs such as LinkedHashSet, LinkedList, and TreeSet, because some of its operations require non-thread-safe objects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.27