CompletableFuture中的anyOf

huangapple go评论78阅读模式
英文:

CompletableFuture anyOf

问题

以下是您提供的内容的翻译:

早些时候我参加了考试,有一个问题是这样的。

> ### 问题5:异步编程(8分)
> 考虑下面的程序。方法 doSomething ( ) 可能会运行不确定的时间。
>
> import java.util.concurrent.CompletableFuture;
> class CF {
> static void doSomething() { .. }
>
> static CompletableFuture<Void> printAsync(int i) {
> return CompletableFuture.runAsync(() -> {
> doSomething();
> System.out.print(i);
> });
> }
>
> public static void main(string[] args) {
> CompletableFuture.anyof(
> printAsync(1)
> .thenRun(() -> printAsync(2)),
> printAsync(3))
> .thenRun(() -> printAsync(4));
> dosomething();
> }
> }
>
> 如果 main 正常运行完成,程序可能打印出什么样的输出?
>
> 用字符串 "yes" 填写空白,如果给定的输出是可能的。用字符串 "no" 填写空白,如果 main 永远不会打印出给定的输出。
> 请注意,这道题将由一个机器人评分。因此,如果填入任何其他文本,如 "NO!","yes,因为...","永远不!" 等,即使答案的意思是正确的,答案也会被判为错误。

> (a) 1
> (b) 2
> (c) 3
> (d) 4
> (e) 12
> (f) 14
> (g) 23
> (h) 24
> (i) 124
> (j) 134
> (k) 243
> (l) 234
> (m) 213
> (n) 1324
> (o) 4321

我的问题是关于 CompletableFuture.anyOf。当其中一个进程完成时,另一个进程是否会在另一个 thenRun 正在运行时完成,还是会提前终止?

英文:

So earlier I had my exam, and there was this question.

> ### Question 5: Asynchronous Programming (8 points)
> Consider the program below. The method doSomething ( ) may run for an undeterministic amount of time.
>
> import java.util.concurrent.CompletableFuture;
> class CF {
> static void doSomething() { .. }
>
> static CompletableFuture<Void> printAsync(int i) {
> return CompletableFuture.runAsync(() -> {
> doSomething();
> System.out.print(i);
> });
> }
>
> public static void main(string[] args) {
> CompletableFuture.anyof(
> printAsync(1)
> .thenRun(() -> printAsync(2)),
> printAsync(3))
> .thenRun(() -> printAsync(4));
> dosomething();
> }
> }
>
> What are the possible outputs printed by the program if main runs to completion normally?
>
> Fill in the blank with the string yes if a given output is possible. Fill in the blank with the string no if main will never print the given output.
> Note that this question will be graded by a bot. So, filling in with any other text, such as "NO!", "yes, because ..", "never!", etc, will lead to the answer being marked as wrong even if the intention of the answer is correct.
>
> (a) 1
> (b) 2
> (c) 3
> (d) 4
> (e) 12
> (f) 14
> (g) 23
> (h) 24
> (i) 124
> (j) 134
> (k) 243
> (l) 234
> (m) 213
> (n) 1324
> (o) 4321

My question is for CompletableFuture.anyOf. When one of the processes finish, does the other process finish to completion even after the other thenRun is running or does it terminate prematurely?

答案1

得分: 1

anyOf仅创建一个新的未来对象,允许安排其他操作,在指定的未来对象中至少有一个完成时执行。它不会影响其任何参数。

但请注意,主方法只使用它来链接另一个操作,而不等待任何未来对象完成。它只是执行另一个doSomething(),该操作需要有意未指定的时间。然后,main方法返回,在标准环境中,只有守护线程存在时,JVM才会终止运行。

由于这种JVM终止不关心任何后台操作,因此无法保证任何操作完成。原则上,甚至可能没有任何输出。

但是链接的操作定义了依赖关系,这些依赖关系会被遵守。

  1. 1 → 2。因此,在输出中您永远不会只看到2而没有1。而且当两个数字都存在时,1必须在2之前。
  2. (1 → 2 | 3) → 4。如果您看到4,则至少必须有23中的一个,并且它必须在4之前。当为2时,第一个点也适用。但是请注意,当23都存在时,只需要其中一个在4之前,因此3412是有效的输出。

除了这些规则外,顺序可以是任意的,正如前面所述,无法保证完成。您可能会看到单独的1或单独的3,或以任意顺序看到13。但是,如上所述,您不会只看到单独的2或单独的4。您可以查看每个建议的答案,并检查是否违反了其中一个依赖关系。如果没有违反,那么它是可能的结果。

英文:

anyOf only creates a new future allowing to schedule other operations, to be performed when at least one of the specified futures has been completed. It does not influence any of its arguments.

But note that the main method only uses it to chain another operation, without waiting for the completion of any future. It just performs another doSomething(), which takes an intentionally unspecified time. Then, the main method returns, and in the standard environment, the JVM will terminate when it detects that only daemon threads exist.

Since this JVM termination does not care for any of the background operations, there is no guaranty about any completion. In principle, even no output is possible.

But the chained operations define dependencies, which are obeyed.

  1. 1 → 2. Therefore, you will never see a 2 without a 1 in the output. And when both numbers are present, 1 must be before 2.
  2. (1 → 2 | 3) → 4. If you see 4, at least either of 2 or 3 must be there, and it must be before 4. When it is 2, the first point applies too. But note that when both, 2 and 3, are present, only one of them is required to be before the 4, so 3412 would be a valid output.

Besides these rules, the order can be arbitrary, and, as said, the completion is not guaranteed. You could see a sole 1 or a sole 3, or 1 and 3 in arbitrary order. But, as explained above, you can’t see a sole 2 nor a sole 4. You can go through every suggested answer and check if it violates one of the dependencies. If not, it’s a possible outcome.

huangapple
  • 本文由 发表于 2020年5月5日 20:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定