Measured parallel processing in Java

I don't know much about Java because it can process in parallel, so I ran Parallel Stream and Completable Future, which I think I often use.

Measurement method

Processing to fetch data from DB on AWS individually with findById from 30 IDs.

Implementation environment






        List<Long> list = createIdList(); //30 cases
        List<Currency> currencies = new ArrayList<>();
        list.forEach(id -> currencyService.findById(id)
                .ifPresent(currency -> currencies.add(currency)));





        List<Long> list = createIdList(); //30 cases
        List<Currency> currencies = new ArrayList<>();
                .forEach(id -> currencyService.findById(id)
                                .ifPresent(currency -> currencies.add(currency)));





        List<Long> list = createIdList(); //30 cases
        List<Currency> currencies = new ArrayList<>();
        ExecutorService ex = Executors.newWorkStealingPool();
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        for (long id : list) {
            futures.add(CompletableFuture.runAsync(() -> currencyService
                    .findById(id).ifPresent(c -> currencies.add(c)), ex));
        CompletableFuture.allOf((futures.toArray(new CompletableFuture[futures.size()]))).join();



By the way, when I tried to make it single thread with ʻExecutors.newSingleThreadExecutor ();`, it was 4123ms.


In this experiment, we are going to get data to RDS, but it seems that we need to be careful when connecting externally with Parallel Stream.

Also, I added it to the List in the loop, but it seems that it is not good because it is a so-called side effect.

Recommended Posts

Measured parallel processing in Java
Parallel execution in Java
Parallel and parallel processing in various languages (Java edition)
Date processing in Java (LocalDate: Initialization)
About file copy processing in Java
Changes in Java 11
Rock-paper-scissors in Java
Java thread processing
Java string processing
[Java] Multi-thread processing
Pi in Java
[Java] Stream processing
FizzBuzz in Java
java iterative processing
Introducing NLP4J-[000] Natural Language Processing Index in Java
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
Use MouseListener in Processing
Express failure in Java
JAVA constructor call processing
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Java random, various processing
Use PreparedStatement in Java
What's new in Java 9,10,11
Initializing HashMap in Java
[Android / Java] Screen transition and return processing in fragments
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Call a program written in Swift from Processing (Java)
Try using RocksDB in Java
Avoid Yubaba's error in Java
[Java] Multi-thread processing --Exclusive control
Get EXIF information in Java
Save Java PDF in Excel
Edit ini in Java: ini4j
Java history in this world
Try calling JavaScript in Java
Try functional type in Java! ①
I made roulette in Java.
Write Processing in IntelliJ IDEA