CompletableFuture.get
时间: 2023-10-27 12:37:58 浏览: 87
20个实例玩转 Java 8 异步神器 CompletableFuture.pdf
CompletableFuture.get() is a method used to retrieve the result of a CompletableFuture. It blocks the current thread until the CompletableFuture completes and returns the result of the computation or throws an exception if the computation failed.
The get() method has two overloaded versions: one without any arguments and another with a timeout parameter. The first version blocks indefinitely until the CompletableFuture completes, whereas the second version blocks for the specified timeout duration and throws a TimeoutException if the CompletableFuture does not complete within that time.
It is important to note that calling the get() method on a CompletableFuture may cause the calling thread to block, which can impact performance and scalability in certain situations. As such, it is recommended to use asynchronous methods like thenApply(), thenCompose(), or handle() instead of get() whenever possible.
阅读全文