0

How to wait for list of futures for 15 minutes( if not completed) and not each future? Below code will wait for each future for 15 minutes.But thats's not what i want

for (CompleteableFuture<String> m : futureList) {
        m.get(15, TimeUnit.MINUTES) ;
}

Syso( " Send email") ;
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Pale Blue Dot
  • 511
  • 2
  • 13
  • 33
  • Maybe you could keep track of how much time passed and only wait for the remaining time in subsequent iterations? – Amongalen Apr 28 '20 at 12:44
  • If switching to Guava is an option to you then you could use `com.google.common.util.concurrent.Futures#allAsList(ListenableFuture...)` – michid Apr 28 '20 at 12:54
  • Yes actually i am getting List>> myKafkaFutures But allAsList doesn't have option of passing Time – Pale Blue Dot Apr 28 '20 at 14:39

1 Answers1

1

Use CompletableFuture.allOf() like so:

       CompletableFuture.allOf(futureList).get(15, TimeUnit.MINUTES);
michid
  • 10,536
  • 3
  • 32
  • 59