Tag: 未来

如何使用CompletableFuture.thenComposeAsync()?

鉴于: public class Test { public static void main(String[] args) { int nThreads = 1; Executor e = Executors.newFixedThreadPool(nThreads); CompletableFuture.runAsync(() -> { System.out.println(“Task 1. Thread: ” + Thread.currentThread().getId()); }, e).thenComposeAsync((Void unused) -> { return CompletableFuture.runAsync(() -> { System.out.println(“Task 2. Thread: ” + Thread.currentThread().getId()); }, e); }, e).join(); System.out.println(“finished”); } } 我期待一个执行程序线程运行任务1,然后执行任务2.相反,如果nThreads小于2,代码将挂起。 请解释代码挂起的原因。 我可以看到它在CompletableFuture中被阻止:616等待一些Future完成,但目前尚不清楚原因。 如果我允许使用2个线程,那么每个线程用于什么? 简而言之,请帮助我理解thenComposeAsync()实际上是如何工作的。 […]

如何创建异步堆栈跟踪?

更新 :Intellij IDEA的最新版本实现了我正在寻找的东西。 问题是如何在IDE之外实现这一点(因此我可以将异步堆栈跟踪转储到日志文件中),理想情况下不使用检测代理。 自从我将应用程序从同步模型转换为异步模型后,我遇到了调试失败的问题。 当我使用同步API时,我总是在exception堆栈跟踪中找到我的类,所以我知道从哪里开始查找是否出错。 使用异步API,我得到的堆栈跟踪不会引用我的类,也不会指示哪个请求触发了失败。 我将举一个具体的例子,但我对这类问题的一般解决方案感兴趣。 具体例子 我使用Jersey发出HTTP请求: new Client().target(“http://test.com/”).request().rx().get(JsonNode.class); 其中rx()表示请求应异步发生,直接返回CompletionStage而不是JsonNode 。 如果此调用失败,我会得到此堆栈跟踪: javax.ws.rs.ForbiddenException: HTTP 403 Authentication Failed at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1083) at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:883) at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$1(JerseyInvocation.java:767) at org.glassfish.jersey.internal.Errors.process(Errors.java:316) at org.glassfish.jersey.internal.Errors.process(Errors.java:298) at org.glassfish.jersey.internal.Errors.process(Errors.java:229) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:765) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:456) at org.glassfish.jersey.client.JerseyCompletionStageRxInvoker.lambda$method$1(JerseyCompletionStageRxInvoker.java:70) at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590) 注意: 堆栈跟踪不引用用户代码。 exception消息不包含有关触发错误的HTTP请求的上下文信息(HTTP方法,URI等)。 因此,我无法将exception追溯到其来源。 为什么会这样 如果你在引擎盖下挖掘,你会发现泽西岛正在调用 : CompletableFuture.supplyAsync(() -> getSyncInvoker().method(name, entity, responseType)) 对于rx()调用。 […]

从Completable Future中的lambda投掷时未报告的exception

当我编译下面的代码时,我收到以下错误: /home/prakashs/composite_indexes/src/main/java/com/spakai/composite/TwoKeyLookup.java:22: error: unreported exception NoMatchException; must be caught or declared to be thrown CompletableFuture<Set> result = calling.thenCombine(called, (s1, s2) -> findCommonMatch(s1, s2)); 代码: public CompletableFuture<Set> lookup(K callingNumber, K calledNumber) throws NoMatchException { CompletableFuture<Set> calling = callingNumberIndex.exactMatch(callingNumber); CompletableFuture<Set> called = calledNumberIndex.exactMatch(calledNumber); CompletableFuture<Set> result = calling.thenCombine(called, (s1, s2) -> findCommonMatch(s1, s2)); return result; } public Set […]