Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持CompletableFuture运行多个Action #50

Closed
1 of 2 tasks
huhaosumail opened this issue Aug 21, 2023 · 2 comments
Closed
1 of 2 tasks

支持CompletableFuture运行多个Action #50

huhaosumail opened this issue Aug 21, 2023 · 2 comments
Assignees

Comments

@huhaosumail
Copy link
Member

huhaosumail commented Aug 21, 2023

目前CompletableFuture提供的都是最多两个入参的方法。

CompletableFuture<Map> future1 = CompletableFuture.supplyAsync(() -> {
    Map result = Maps.newHashMap();
    result.put("j", "j");
    return result;
});

future1.thenAccept((Map map) -> {
    System.out.println("thenAccept:" + map.get("j"));
});

future1.thenCombine(CompletableFuture.completedFuture("123"), (map, s) -> {
    System.out.println("thenCombine:" + map.get("j"));
    System.out.println(s);
    return "yy";
});

能否提供操作多个入参的工具类方法?
例如多个入参的消费accept/combine

implement rest related methods

@oldratlee
Copy link
Member

oldratlee commented May 28, 2024

能否提供操作多个入参的工具类方法?

@huhaosumail 目前(v1.0.0-Alpha7)实现支持了 多个输入并返回多个输入结果的方法,如allResultsOf/allTupleOfmostResultsOfSuccess/mostTupleOfSuccessallResultsOfFastFail/allTupleOfFastFail

但这些方法,输入的类型是CompletionStage,而不是action类型(如RunnableConsumerFunction)。CompletionStage类型参数。

在常见简单业务场景的使用中,并不方便与安全。期望的使用方式,代码示例如下:

////////////////////////////////////////
// Demo 1
////////////////////////////////////////

// 目前功能的使用方式,awkward~ 😖
CompletableFuture.allResultsOf(
    CompletableFuture.supplyAsync(() -> 42),
    CompletableFuture.supplyAsync(() -> 52),
    CompletableFuture.supplyAsync(() -> 62)
).thenAccept(System.out::println);

// <=>

// 期望的使用方式,fresh and cool 😋
CompletableFuture.mSupplyAsync(
    () -> 42,
    () -> 52,
    () -> 62
).thenAccept(System.out::println);
// output: [42, 52, 62]


////////////////////////////////////////
// Demo 2
////////////////////////////////////////

// 目前功能的使用方式,awkward~ 😖
CompletableFuture.supplyAsync(() -> 42).thenCompose(v ->
    allResultsOf(
        CompletableFuture.supplyAsync(() -> v + 1),
        CompletableFuture.supplyAsync(() -> v + 2),
        CompletableFuture.supplyAsync(() -> v + 3)
    )
).thenAccept(System.out::println);

// <=>

// 期望的使用方式,fresh and cool 😋
CompletableFuture.supplyAsync(() -> 42).thenMApplyAsync(
    v -> v + 1,
    v -> v + 2,
    v -> v + 3
).thenAccept(System.out::println);
// output: [43, 44, 45]

实现输入参数是action类型的多参数方法,后续版本高优 ❤️ 😄 @huhaosumail

@oldratlee oldratlee added the ✨ feature New feature label May 28, 2024
@oldratlee oldratlee changed the title CompletableFuture增强 支持CompletableFuture运行多个Action May 31, 2024
@oldratlee
Copy link
Member

oldratlee commented Jun 18, 2024

@huhaosumailv1.0.0-Alpha10版本中,实现了:

  • implement the multi-actions methods in CompletableFutureUtils ✨ 🚀
    • mSupplyAsync/mSupplyFastFailAsync/mSupplyMostSuccessAsync
    • mRunAsync/mRunFastFailAsync
    • thenMApplyAsync/thenMApplyFastFailAsync/thenMApplyMostSuccessAsync
    • thenMAcceptAsync/thenMAcceptFastFailAsync
    • thenMRunAsync/thenMRunFastFailAsync

还要继续补全实现相应的方法:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants