Каков рекомендуемый способ ожидания завершения завершаемых будущих потоков?
я используюCompletableFuture
как показано ниже в коде. Но что касается того, как я должен ждать, пока не закончатся все работающие объекты, я нашел два пути, и я не знаю разницу между ними, и какой из них является лучшим? Они заключаются в следующем:
Код:
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);
Первый подход - дождаться окончания всех работ:
this.growSeedExecutor.shutdown();
this.growSeedExecutor.awaitTermination(1, TimeUnit.DAYS);
Второй подход - дождаться окончания всех работ:
CompletableFuture.allOf(this.growSeedFutureList).join();
Пожалуйста, дайте мне знать, какой из них рекомендуется.