I am using CompletableFuture as shown below in the code. but as to how I should wait until all runnables are finished, I found two ways, and I donโt know the difference between them, and which one is the best practice? they are as follows:
the code
this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor); this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
first approach to wait for all runnables to complete
this.growSeedExecutor.shutdown(); this.growSeedExecutor.awaitTermination(1, TimeUnit.DAYS);
second approach to wait for all runnables to complete
CompletableFuture.allOf(this.growSeedFutureList).join();
please let me know which one is recommended
java multithreading concurrency completable-future
user2121
source share