学习 JDK CompletableFeature API的案例;
包含各种API的使用场景案例和解释说明;
|
|
преди 1 седмица | |
|---|---|---|
| src | преди 1 седмица | |
| .gitignore | преди 1 седмица | |
| AGENTS.md | преди 1 седмица | |
| README.md | преди 1 седмица | |
| completablefuture-blog.md | преди 1 седмица | |
| completablefuture-tutorial.md | преди 1 седмица | |
| pom.xml | преди 1 седмица | |
| template.md | преди 1 седмица | |
| todo.md | преди 1 седмица |
一个用于学习 JDK CompletableFuture API 的 Java 17 / Maven 教学项目,包含配套的中文教程文档与可直接运行的代码示例(Cf01..Cf10)。
completableFeature-demo/
├── src/main/java/space/anyi/ # 示例代码,每个 API 主题一个文件,均有独立 main()
├── completablefuture-tutorial.md # 中文教程文档(循序渐进,配合示例代码)
├── completablefuture-blog.md # 面向资深 Java 程序员的速查/博客文档
├── template.md # 写作风格与结构参考文档
└── todo.md # 待办任务清单
| 文件 | 主题 |
|---|---|
Cf01_CreateStage |
创建异步任务:runAsync / supplyAsync / completedFuture / getNow |
Cf02_ThenSeries |
串行处理:thenApply / thenAccept / thenRun |
Cf03_ThenCombine |
组合两个任务:thenCombine / thenAcceptBoth / runAfterBoth |
Cf04_ThenEither |
二选一:applyToEither / acceptEither / runAfterEither |
Cf05_ThenCompose |
展平组合:thenCompose |
Cf06_AllOfAnyOf |
批量聚合:allOf / anyOf |
Cf07_ExceptionHandling |
异常处理:exceptionally / whenComplete / handle |
Cf08_ManualComplete |
手动完成与状态查询:complete / completeExceptionally / obtrudeValue / isDone / cancel |
Cf09_CustomExecutor |
线程池与 Async 变体 |
Cf10_TimeoutAndException |
超时与异常链:orTimeout / completeOnTimeout / exceptionallyCompose |
详细讲解见 completablefuture-tutorial.md。
mvn compile # 编译
mvn exec:java -Dexec.mainClass=space.anyi.Cf01_CreateStage # 运行某个示例
exec:java 需要显式指定 -Dexec.mainClass。-Dexec.mainClass=... 拆分,需要加引号:'-Dexec.mainClass=space.anyi.Cf01_CreateStage'。Cf10(使用 orTimeout / completeOnTimeout)在 exec:java 下会因 JDK 延迟调度线程残留而报线程组清理错误,属误报。请改用普通方式运行:
java -cp target/classes space.anyi.Cf10_TimeoutAndException