首先,很明显,我对thenApply
不太了解,这就是为什么我遇到编译器错误,但我尝试搜索但无法搜索。
这是我的简单代码:
import java.util.concurrent.CompletionStage;
public class Main5 {
public static void main(String[] args) {
}
static class Game {
public void doIt(int id) {
CompletionStage<Player> player = getPlayer(id).thenApplyAsync(p -> {
modifyPlayer(p.getId());
return getPlayer(p.getId());
});
}
private CompletionStage<Player> getPlayer(int id) {
//do http request to get the player info
}
private CompletionStage<Void> modifyPlayer(int id) {
//do http request to modify player's info
}
}
}
在thenApplySync
中,我收到此编译器错误:
incompatible types: inference variable U has incompatible bounds
equality constraints: com.testapp.Player
lower bounds: java.util.concurrent.CompletionStage<com.testapp.Player>
getPlayer
方法已经返回完成阶段,那怎么了?
我知道
如果我这样做:return getPlayer(p.getId()).toCompletableFuture().get();
可以工作,但是我不明白为什么。在我看来,我应该返回完成阶段,而不是对象。
感谢您的帮助。