Skip to content

Commit c71f34b

Browse files
committed
docs: improve wording of README and javadoc 📚
1 parent 4ae56f4 commit c71f34b

File tree

5 files changed

+94
-50
lines changed

5 files changed

+94
-50
lines changed

README.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# <div align="center"><a href="#dummy"><img src="https://github.com/foldright/cffu/assets/1063891/124658cd-025f-471e-8da1-7eea0e482915" alt="🦝 CompletableFuture-Fu(CF-Fu)"></a></div>
22

33
<p align="center">
4-
<a href="https://github.com/foldright/cffu/actions/workflows/fast_ci.yaml"><img src="https://img.shields.io/github/actions/workflow/status/foldright/cffu/fast_ci.yaml?branch=main&logo=github&logoColor=white&label=fast ci" alt="Fast CI - GH Workflow Build Status"></a>
5-
<a href="https://github.com/foldright/cffu/actions/workflows/ci.yaml"><img src="https://img.shields.io/github/actions/workflow/status/foldright/cffu/ci.yaml?branch=main&logo=github&logoColor=white&label=strong ci" alt="Strong CI - GH Workflow Build Status"></a>
4+
<a href="https://github.com/foldright/cffu/actions/workflows/fast_ci.yaml"><img src="https://img.shields.io/github/actions/workflow/status/foldright/cffu/fast_ci.yaml?branch=main&logo=github&logoColor=white&label=fast%20ci" alt="Fast Build CI"></a>
5+
<a href="https://github.com/foldright/cffu/actions/workflows/ci.yaml"><img src="https://img.shields.io/github/actions/workflow/status/foldright/cffu/ci.yaml?branch=main&logo=github&logoColor=white&label=strong%20ci" alt="Strong Build CI"></a>
66
<a href="https://app.codecov.io/gh/foldright/cffu/tree/main"><img src="https://img.shields.io/codecov/c/github/foldright/cffu/main?logo=codecov&logoColor=white" alt="Codecov"></a>
7-
<a href="https://qodana.cloud/projects/A61Yy"><img src="https://img.shields.io/github/actions/workflow/status/foldright/cffu/qodana_code_quality.yml?branch=main&logo=jetbrains&logoColor=white&label=qodana" alt="Qodana - GH Workflow Build Status"></a>
7+
<a href="https://qodana.cloud/projects/A61Yy"><img src="https://img.shields.io/github/actions/workflow/status/foldright/cffu/qodana_code_quality.yml?branch=main&logo=jetbrains&logoColor=white&label=qodana" alt="Qodana Code Inspections"></a>
88
<a href="https://openjdk.java.net/"><img src="https://img.shields.io/badge/Java-8+-339933?logo=openjdk&logoColor=white" alt="Java support"></a>
99
<a href="https://www.apache.org/licenses/LICENSE-2.0.html"><img src="https://img.shields.io/github/license/foldright/cffu?color=4D7A97&logo=apache" alt="License"></a>
1010
<a href="https://foldright.io/api-docs/cffu/"><img src="https://img.shields.io/github/release/foldright/cffu?label=javadoc&color=339933&logo=read-the-docs&logoColor=white" alt="Javadocs"></a>
@@ -472,31 +472,52 @@ public class ConcurrencyStrategyDemo {
472472
473473
### 2.4 支持直接运行多个`Action`,而不是要先包装成`CompletableFuture`
474474

475-
`CompletableFuture``allOf/anyOf`方法输入的是`CompletableFuture`,当业务直接有要编排业务逻辑方法时仍然需要先包装成`CompletableFuture`再运行:
475+
`CompletableFuture``allOf/anyOf`方法输入的是`CompletableFuture`;当业务直接有要编排业务逻辑方法时,仍然需要先包装成`CompletableFuture`再运行:
476476

477477
- 繁琐
478-
- 也模糊了业务流程
478+
- 模糊了业务流程
479+
- 简单包装多个`Action``CF`提交给`allOf/anyOf`的做法(业务代码往/往是这样实现的),**会呑异常**
480+
- 当输入`Action`的运行抛出多个异常时,这些异常至多只能有一个能通过返回`CF`反馈给业务,其它的异常则被默默地呑掉,影响业务问题的排查
479481

480-
`cffu`提供了直接运行多个`Action`的方法,方便直接明了地表达业务编排流程。
482+
`cffu`提供了直接运行多个`Action`的方法,解决上述问题:
483+
484+
- 方便直接明了地表达与编排业务流程
485+
- 不呑异常,方便排查业务问题
486+
- 当多个输入`Action`的运行抛出多个异常时,会打印日志报告出没有在返回`CF`中反馈给业务的异常
481487

482488
示例代码如下:
483489

484490
```java
485491
public class MultipleActionsDemo {
492+
private static final ExecutorService myBizExecutor = Executors.newCachedThreadPool();
493+
private static final CffuFactory cffuFactory = CffuFactory.builder(myBizExecutor).build();
494+
486495
static void mRunAsyncDemo() {
487-
// wrap tasks to CompletableFuture first, AWKWARD! 😖
496+
// wrap actions to CompletableFutures first, AWKWARD! 😖
488497
CompletableFuture.allOf(
489498
CompletableFuture.runAsync(() -> System.out.println("task1")),
490499
CompletableFuture.runAsync(() -> System.out.println("task2")),
491500
CompletableFuture.runAsync(() -> System.out.println("task3"))
492501
);
502+
completedFuture("task").thenCompose(v ->
503+
CompletableFuture.allOf(
504+
CompletableFuture.runAsync(() -> System.out.println(v + "1")),
505+
CompletableFuture.runAsync(() -> System.out.println(v + "2")),
506+
CompletableFuture.runAsync(() -> System.out.println(v + "3"))
507+
)
508+
);
493509

494510
// just run multiple actions, fresh and cool 😋
495511
CompletableFutureUtils.mRunAsync(
496512
() -> System.out.println("task1"),
497513
() -> System.out.println("task2"),
498514
() -> System.out.println("task3")
499515
);
516+
cffuFactory.completedFuture("task").thenMAcceptAsync(
517+
(String v) -> System.out.println(v + "1"),
518+
v -> System.out.println(v + "2"),
519+
v -> System.out.println(v + "3")
520+
);
500521
}
501522
}
502523
```
@@ -511,7 +532,7 @@ public class MultipleActionsDemo {
511532
private static final CffuFactory cffuFactory = CffuFactory.builder(myBizExecutor).build();
512533

513534
static void thenMApplyAsyncDemo() {
514-
// wrap tasks to CompletableFuture first, AWKWARD! 😖
535+
// wrap actions to CompletableFutures first, AWKWARD! 😖
515536
completedFuture(42).thenCompose(v ->
516537
CompletableFutureUtils.allResultsFailFastOf(
517538
CompletableFuture.supplyAsync(() -> v + 1),
@@ -521,7 +542,8 @@ public class MultipleActionsDemo {
521542
).thenAccept(System.out::println);
522543
// output: [43, 44, 45]
523544
cffuFactory.completedFuture(42).thenCompose(v ->
524-
CompletableFutureUtils.allResultsFailFastOf(
545+
CompletableFutureUtils.allSuccessResultsOf(
546+
-1,
525547
CompletableFuture.supplyAsync(() -> v + 1),
526548
CompletableFuture.supplyAsync(() -> v + 2),
527549
CompletableFuture.supplyAsync(() -> v + 3)
@@ -537,14 +559,15 @@ public class MultipleActionsDemo {
537559
v -> v + 3
538560
).thenAccept(System.out::println);
539561
// output: [43, 44, 45]
540-
cffuFactory.completedFuture(42).thenMApplyFailFastAsync(
562+
cffuFactory.completedFuture(42).thenMApplyAllSuccessAsync(
563+
-1,
541564
v -> v + 1,
542565
v -> v + 2,
543566
v -> v + 3
544567
).thenAccept(System.out::println);
545568
// output: [43, 44, 45]
546569

547-
CompletableFutureUtils.thenMApplyAllSuccessTupleAsync(
570+
CompletableFutureUtils.thenMApplyTupleFailFastAsync(
548571
completedFuture(42),
549572
v -> "string" + v,
550573
v -> v + 1,

cffu-core/src/main/java/io/foldright/cffu/Cffu.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,9 +1710,9 @@ public Cffu<T> exceptionallyAsync(Function<Throwable, ? extends T> fn, Executor
17101710
* Uses {@link #defaultExecutor()} as {@code executorWhenTimeout}.
17111711
* <p>
17121712
* <strong>CAUTION:</strong> This method returns a new Cffu instead of this Cffu to avoid the subsequent usage of the
1713-
* delay thread; This behavior is DIFFERENT from the original CF method {@link CompletableFuture#orTimeout} and its
1714-
* backport method {@link #unsafeOrTimeout}. More info see the javadoc of {@link #unsafeOrTimeout} and the demo <a href=
1715-
* "https://github.com/foldright/cffu/blob/main/cffu-core/src/test/java/io/foldright/demo/CfDelayDysfunctionDemo.java"
1713+
* delay thread; This behavior is DIFFERENT from the original CF method {@link CompletableFuture#orTimeout CompletableFuture#orTimeout}
1714+
* and its backport method {@link #unsafeOrTimeout unsafeOrTimeout}. More info see the javadoc of {@link #unsafeOrTimeout unsafeOrTimeout}
1715+
* and the demo <a href="https://github.com/foldright/cffu/blob/main/cffu-core/src/test/java/io/foldright/demo/CfDelayDysfunctionDemo.java"
17161716
* >DelayDysfunctionDemo</a>.
17171717
*
17181718
* @param timeout how long to wait before completing exceptionally with a TimeoutException, in units of {@code unit}
@@ -1734,17 +1734,17 @@ public Cffu<T> orTimeout(long timeout, TimeUnit unit) {
17341734
* This means that the long-running subsequent non-async actions will block this executor thread, preventing it from
17351735
* handling other timeouts and delays, effectively breaking CompletableFuture's timeout and delay functionality.
17361736
* <p>
1737-
* <strong>Strongly recommend</strong> using the safe method {@link #orTimeout(long, TimeUnit)} instead of this method.
1738-
* Using this method is appropriate only when:
1737+
* <strong>Strongly recommend</strong> using the safe method {@link #orTimeout(long, TimeUnit) orTimeout}
1738+
* instead of this method. Using this method is appropriate only when:
17391739
* <ul>
17401740
* <li>the returned Cffu is only read explicitly(e.g. by get/join/resultNow methods), and/or
17411741
* <li>all subsequent actions of dependent Cffus/CompletableFutures are guaranteed to execute asynchronously
17421742
* (i.e., the dependent Cffus/CompletableFutures are created using async methods).
17431743
* </ul> In these cases, using this unsafe method avoids an unnecessary thread switch when timeout occurs; However,
17441744
* these conditions are difficult to guarantee in practice especially when the returned Cffu is used by others' codes.
17451745
* <p>
1746-
* Note: Before Java 21(Java 20-), {@link CompletableFuture#orTimeout} leaks if the future completes exceptionally,
1747-
* more info see <a href="https://bugs.openjdk.org/browse/JDK-8303742">issue JDK-8303742</a>,
1746+
* Note: Before Java 21(Java 20-), {@link CompletableFuture#orTimeout CompletableFuture#orTimeout} leaks if the future
1747+
* completes exceptionally, more info see <a href="https://bugs.openjdk.org/browse/JDK-8303742">issue JDK-8303742</a>,
17481748
* <a href="https://github.com/openjdk/jdk/pull/13059">PR review openjdk/jdk/13059</a>
17491749
* and <a href="https://github.com/openjdk/jdk/commit/ded6a8131970ac2f7ae59716769e6f6bae3b809a">JDK bugfix commit</a>.
17501750
* The cffu backport logic(for Java 20-) has merged this JDK bugfix.
@@ -1770,9 +1770,9 @@ public Cffu<T> unsafeOrTimeout(long timeout, TimeUnit unit) {
17701770
* <p>
17711771
* <strong>CAUTION:</strong> This method returns a new Cffu instead of this Cffu
17721772
* to avoid the subsequent usage of the delay thread; This behavior is DIFFERENT from the original CF method
1773-
* {@link CompletableFuture#completeOnTimeout} and its backport method {@link #unsafeCompleteOnTimeout}.
1774-
* More info see the javadoc of {@link #unsafeCompleteOnTimeout} and the demo <a href=
1775-
* "https://github.com/foldright/cffu/blob/main/cffu-core/src/test/java/io/foldright/demo/CfDelayDysfunctionDemo.java"
1773+
* {@link CompletableFuture#completeOnTimeout CompletableFuture#completeOnTimeout} and its backport method {@link
1774+
* #unsafeCompleteOnTimeout unsafeCompleteOnTimeout}. More info see the javadoc of {@link #unsafeCompleteOnTimeout} and the demo
1775+
* <a href="https://github.com/foldright/cffu/blob/main/cffu-core/src/test/java/io/foldright/demo/CfDelayDysfunctionDemo.java"
17761776
* >DelayDysfunctionDemo</a>.
17771777
*
17781778
* @param value the value to use upon timeout
@@ -1794,8 +1794,8 @@ public Cffu<T> completeOnTimeout(@Nullable T value, long timeout, TimeUnit unit)
17941794
* This means that the long-running subsequent non-async actions will block this executor thread, preventing it from
17951795
* handling other timeouts and delays, effectively breaking CompletableFuture's timeout and delay functionality.
17961796
* <p>
1797-
* <strong>Strongly recommend</strong> using the safe method {@link #completeOnTimeout(Object, long, TimeUnit)}
1798-
* instead of this method. Using this method is appropriate only when:
1797+
* <strong>Strongly recommend</strong> using the safe method {@link #completeOnTimeout(Object, long, TimeUnit)
1798+
* completeOnTimeout} instead of this method. Using this method is appropriate only when:
17991799
* <ul>
18001800
* <li>the returned Cffu is only read explicitly(e.g. by get/join/resultNow methods), and/or
18011801
* <li>all subsequent actions of dependent Cffus/CompletableFutures are guaranteed to execute asynchronously

cffu-core/src/main/java/io/foldright/cffu/CffuFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,8 @@ public <T> CompletionStage<T> failedStage(Throwable ex) {
12281228
////////////////////////////////////////////////////////////////////////////////
12291229

12301230
/**
1231-
* Returns a Cffu maintaining the same completion properties as the given stage and this {@code CffuFactory} config.
1232-
* If the given stage is already a Cffu and have the this {@code CffuFactory}, this method may return the given stage.
1231+
* Returns a Cffu that maintains the same completion properties as the given stage, configured with this {@code CffuFactory}.
1232+
* If the given stage is already a Cffu and uses this {@code CffuFactory}, this method may return the given stage.
12331233
*
12341234
* @throws NullPointerException if the given stage is null
12351235
* @see CompletionStage#toCompletableFuture()

0 commit comments

Comments
 (0)