线程在 thenAccept 中被阻止,但在 thenAcceptAsync 中工作

Thread gets blocked in thenAccept but works in thenAcceptAsync

提问人:Saksham Gupta 提问时间:4/21/2023 更新时间:4/21/2023 访问量:28

问:

我正在使用 Helidon WebClient 进行 API 调用,我遇到了某个用例,我正在粘贴该用例的演示。

        var request = webClient.get()
                .path("/api/users?page=2")
                .request(String.class)
                .thenAccept(res -> {
                    printThreadId("firstreq");
                    
                    System.out.println(res);
                    
                    var res2 = webClient.get()
                            .path("/api/users?page=2")
                            .request(String.class)
                            .await();

                    System.out.println(res2);

                    cf.complete(null);
                });

问题是线程在 res2 调用后被阻塞了无休止的时间。

解决方案是使用 thenAcceptAsync 而不是 thenAccept。但是,我无法理解,为什么会这样?

java 异步 回调 completable-future helidon-webclient

评论

0赞 Marcono1234 4/23/2023
不熟悉 Helidon 也不知道您是如何配置的,但我的假设是它使用单个线程,因此当在该线程中运行时并且它本身尝试使用它时会卡住,因为该线程已经在使用中(本身)。而对于它来说,它可能使用具有更多/其他线程(?如有疑问,请使用调试器暂停执行,并查看它停滞的位置。正确的解决方案可能是不调用,而是从嵌套调用中返回,然后链接更多。webClientthenAcceptwebClientthenAcceptthenAcceptAsyncawait()CompletionStagerequest

答: 暂无答案