Wait until any of Future is done

前端 未结 8 1079
长发绾君心
长发绾君心 2021-02-01 01:09

I have few asynchronous tasks running and I need to wait until at least one of them is finished (in the future probably I\'ll need to wait util M out of N tasks are finished). C

8条回答
  •  生来不讨喜
    2021-02-01 01:33

    If you can use CompletableFutures instead then there is CompletableFuture.anyOf that does what you want, just call join on the result:

    CompletableFuture.anyOf(futures).join()
    

    You can use CompletableFutures with executors by calling the CompletableFuture.supplyAsync or runAsync methods.

提交回复
热议问题