为什么thenApply和thenApplySync无法在完成阶段使用 我知道

首先,很明显,我对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();可以工作,但是我不明白为什么。在我看来,我应该返回完成阶段,而不是对象。

感谢您的帮助。

zhichaoc21614879 回答:为什么thenApply和thenApplySync无法在完成阶段使用 我知道

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3128920.html

大家都在问