Reputation: 7228
I use spring boot version 2.1.9.RELEASE along with Java 1.8 and have two lang running processes which I wanted to be started in parallel. Therefore I have decided to use thread. When I start the sumResult method, the second thread starts first and the first thread wait until the second one is finished.
Why these two threads does not start at the same time or at least short after each other?
private void sumResult(String year, String month, String day) throws
ExecutionException, InterruptedException {
ExecutorCompletionService<Boolean> completionService = new
ExecutorCompletionService<>(Executors.newCachedThreadPool());
// First thread
mut.initialise(year, month, day);
boolean mutCompleted = completionService.submit(
()-> mut.sum(),true).get();
// Second thread
apt.initialise(year, month, day);
boolean aptCompleted = completionService.submit(
()-> apt.sum(), true).get();
// On completion of both thread
if(mutCompleted && aptCompleted ){
mixAndPrint();
}
}
Upvotes: 0
Views: 244
Reputation: 262504
Because you are blocking on calling get()
on the first job before even submitting the second one.
submit
get
submit
get
If you want them to run in parallel, you need to do
submit
submit
get
get
Upvotes: 4