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.
Processing to fetch data from DB on AWS individually with findById from 30 IDs.
local
AWS RDS
Serial
List<Long> list = createIdList(); //30 cases
List<Currency> currencies = new ArrayList<>();
list.forEach(id -> currencyService.findById(id)
.ifPresent(currency -> currencies.add(currency)));
result
3644ms
ParallelStream
ParallelStream
List<Long> list = createIdList(); //30 cases
List<Currency> currencies = new ArrayList<>();
list.parallelStream()
.forEach(id -> currencyService.findById(id)
.ifPresent(currency -> currencies.add(currency)));
result
607ms
CompletableFuture
CompletableFuture
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();
result
604ms
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. http://taichiw.hatenablog.com/entry/2018/09/30/224907
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