问题标签 [completable-future]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
6 回答
23206 浏览

java - 如何取消 Java 8 可完成的未来?

我正在玩 Java 8 可完成的期货。我有以下代码:

使用 runAsync 我安排执行等待闩锁的代码。接下来我取消未来,期望内部抛出一个中断的异常。但似乎线程在 await 调用上仍然被阻塞,即使未来被取消(断言通过),也永远不会抛出 InterruptedException。使用 ExecutorService 的等效代码按预期工作。它是 CompletableFuture 还是我的示例中的错误?

0 投票
2 回答
6876 浏览

java - CompletableFuture withFallback / 只处理一些错误

我正在通过 CompletableFuture 接收来自服务调用的响应。我想处理服务返回的一些已知异常——例如乐观并发控制冲突。

这就是我所拥有的。有没有更好的方法来做到这一点,它不包装异常或使用 SneakyThrows?包装异常意味着其他异常处理程序必须检查因果链,而不仅仅是使用instanceof.

同样,有没有一种方法可以在没有 wrap-unwrap 的情况下复制番石榴的 withFallback ?


为了完整起见,如果我允许包装异常,这就是它的样子。(我有一个单元测试来验证抛出的异常沿链传播):

0 投票
2 回答
48239 浏览

exception - Java 8 CompletableFuture 异常方法的惊人行为

我遇到了 Java 8 CompletableFuture.exceptionally 方法的奇怪行为。如果我执行此代码,它可以正常工作并打印java.lang.RuntimeException

但是,如果我在以后的处理中添加另一个步骤,例如thenApply,异常类型将更改为java.util.concurrent.CompletionException包含在内部的原始异常。

有什么理由应该发生这种情况吗?在我看来,这非常令人惊讶。

0 投票
2 回答
1375 浏览

java - Why does thenComposeAsync await the return to be redeemable

I have written up a contrived code example, and it might not be code that someone ought to use, but I believe it should work. However it instead deadlocks. I've read the answers described here, but found them insufficient.

Here is the code example:

#xA;

This prints:

About to enqueue task

Task enqueued

And then it hangs. It's deadlocked because the executor only has a single thread, and it's waiting for the innerFuture to become redeemable. Why does "thenComposeAsync" block for its return value to become redeemable, instead of returning the still-incomplete future and freeing up its thread in the executor?

This feels completely unintuitive, and the javadocs don't really help. Am I fundamentally misunderstanding how CompletionStages work? Or is this a bug in the implementation?

0 投票
2 回答
55993 浏览

multithreading - CompletableFuture、supplyAsync() 和 thenApply()

需要确认一些东西。以下代码:

将与以下内容相同:

对?

此外,另外两个问题是“我们有什么理由要使用thenApply吗?”

1)有大代码进行转换?

或者

2) 需要在其他地方重用 lambda 块?

0 投票
1 回答
971 浏览

multithreading - Java 8 CompletedFuture 网络爬虫不会爬过一个 URL

我正在使用 Java 8 中新引入的并发特性,工作练习来自 Cay S. Horstmann 的“Java SE 8 for the really Impatient”一书。我使用新的CompletedFuturejsoup创建了以下网络爬虫。基本思想是给定一个 URL,它会在该页面上找到前m个URL,并重复该过程n次。当然, mn是参数。问题是程序获取初始页面的 URL 但不递归。我错过了什么?

测试用例:

0 投票
2 回答
638 浏览

multithreading - Java 8 CompletedFuture 和 ThreadLocalRandom

我正在练习 Cay S. Horstmann 所著的“真正不耐烦的 Java SE 8”一书。

写一个方法

异步重复操作,直到它产生一个函数接受的值,该until函数也应该异步运行。使用从控制台读取 a java.net.PasswordAuthentication的函数以及通过休眠一秒钟然后检查密码是否为“秘密”来模拟有效性检查的函数进行测试。

我想出了以下代码,但随机密码生成策略似乎让我失望了。所有线程不断选择相同的密码,这看起来很奇怪。

测试用例:

运行一次,看看选择相同的密码有多奇怪:

2015-01-09 15:41:33.350 [Thread-1] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:34.371 [Thread-3] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:35.442 [Thread-5] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:36.443 [Thread-7] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。2015-01-09 15:41:37.451 [Thread-9] [INFO] najjcPracticeQuestionsCh6Test - 收到密码:secretPassword。

0 投票
3 回答
3379 浏览

java - CompletableFuture recoverWith 等价物?即异常但返回 CompletableFuture

0 投票
4 回答
4064 浏览

java - Java 8 可完成的未来

我的问题是如何使用 Completable Future。

我有一个实现 Callable 的类。

早先是用来做 -

这将返回一个Future<Collection>. 但是我们不想再使用 future 并且需要CompletableFuture. 一个想法是我们不需要使用 CompletableFuture 进行轮询,也不需要等待和阻塞,直到它准备好。

那么我将如何使用可完成的未来并isDone()callable线程完成时调用一个函数。

0 投票
5 回答
17243 浏览

java - 使用 CompletableFuture 处理 Java 8 供应商异常

考虑以下代码

上面的东西工作正常。但是sendNumbers,在我的情况下也可能引发检查异常,例如:

现在我想像y在我的biConsumer. 这将帮助我在单个函数 ( ) 中处理结果以及异常(如果有biConsumer)。

有任何想法吗?我可以CompletableFuture.exceptionally(fn)在这里或其他地方使用吗?