| Processing stream data in parallel: Execution and benchmarking |
|
|
| Week of April 25, 2022 |
| Also, interfaces, sealed classes, and lazy Java code |
|
|
|
|
|
The Stream API is arguably best known for reading information sequentially from a datasource. However, given the multithreaded capabilities of most target devices, streaming data in parallel may be more efficient.
Note that important word may.
As Khalid Mughal and Vasily Strelnikov explain, a parallel stream is split into multiple substreams that are processed in parallel by multiple instances of the stream pipeline being executed by multiple threads, and their intermediate results are combined to create the final result.
However, while it’s easy to set the Stream API to parallel mode, there are potential pitfalls to avoid when you’re executing stream pipelines in parallel. Under some circumstances, there won’t be a performance improvement. Thus, for any nontrivial production application, you should benchmark with your own code, with the typical data the software might see, and with the target platform. See how parallel streams work—and explore the code for several ready-to-adopt benchmarking methods.
Take care, Alan Zeichick Editor in Chief, Java Magazine @zeichick
P.S. Are you a MySQL fan? See the latest version of Oracle MySQL HeatWave, the only MySQL cloud service with a built-in, high-performance, in-memory query accelerator. |
|
| The newest Java Magazine articles |
Parallel streams in Java: Benchmarking and performance considerations The Stream API makes it possible to execute a sequential stream in parallel without rewriting the code. The primary reason for using parallel streams is to improve performance while at the same time ensuring that the results obtained are the same, or at least compatible, regardless of the mode of execution. Khalid Mughal and Vasily Strelnikov show how that works in the real world.
Bruce Eckel on Java interfaces and sealed classes With the introduction of default and static methods in interfaces, Java made it possible to write method code in an interface that you might not want to be public. Also, JDK 17 finalizes the introduction of sealed classes and interfaces, so the base class or interface can constrain what classes can be derived from it. Bruce Eckel continues his series on relatively new Java features by looking at sealed classes and interfaces.
Lazy Java code makes applications elegant and sophisticated Venkat Subramaniam believes that laziness is the secret to success, both in life and in programming. Pretend that your boss tells you on January 10 that a certain hour-long task must be done before April 15. A go-getter might do that task by January 11. However, aiming to complete the work superquickly is not the best approach, insists Subramaniam. Instead, be lazy. Don’t complete that task until April 14. Read how that principle applies to Java development. |
|
| Popular articles about switch statements and expressions |
|
|
|
| Complimentary subscription to Java Magazine |
Java Magazine is a deep dive into Java and the JVM. Find detailed explanations about the language and the platform written by experts and members of the Java development team.
Join a quarter of a million subscribers in getting useful, authoritative programming information delivered directly to your inbox. |
|
|
|
|
|