CompletableFuture.supplyAsync
时间: 2023-10-13 21:03:55 浏览: 107
CompletableFuture.supplyAsync is a method in Java's CompletableFuture class that allows you to create a CompletableFuture with a supplier function. This method is typically used when you want to asynchronously execute a task and obtain its result. The supplier function is responsible for providing the result value.
Here's an example of how to use CompletableFuture.supplyAsync:
```java
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
// Perform some task to generate a result
return "Hello, world!";
});
// You can then use the CompletableFuture to perform other actions, such as applying transformations or handling the result
future.thenApply(result -> {
// Manipulate the result
return result.toUpperCase();
}).thenAccept(result -> {
// Consume the manipulated result
System.out.println(result);
});
```
In this example, the supplyAsync method is used to asynchronously execute a task that returns the string "Hello, world!". The thenApply method is then used to transform the result to uppercase, and finally, the thenAccept method is used to consume and print the manipulated result.
I hope this answers your question! Let me know if you have any more.
阅读全文