然后将其应用于一个compleableFuture [英] Multiple thenApply in a completableFuture

查看:61
本文介绍了然后将其应用于一个compleableFuture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我想在不同的线程中执行某些方法,但想将一个线程的结果传递给另一个线程。我的课堂上有以下方法。

I have a situation where I want to execute some methods in different threads but want to pass the result of one thread to another. I have following methods in my class.

public static int addition(int a, int b){
    System.out.println((a+b));
    return (a+b);
}

public static int subtract(int a, int b){
    System.out.println((a-b));
    return (a-b);
}

public static int multiply(int a, int b){
    System.out.println((a*b));
    return (a*b);
}
public static String convert(Integer a){
    System.out.println((a));
    return a.toString();
}

这是主要方法:

public static void main(String[] args) {
    int a = 10;
    int b = 5;
    CompletableFuture<String> cf = new CompletableFuture<>();
    cf.supplyAsync(() -> addition(a, b))
        .thenApply(r ->subtract(20,r)
                .thenApply(r1 ->multiply(r1, 10))
                .thenApply(r2 ->convert(r2))
                .thenApply(finalResult ->{
                    System.out.println(cf.complete(finalResult));
                }));
    System.out.println(cf.complete("Done"));

}

我正在尝试将加减的结果传递给乘法打印结果。但是我遇到编译错误。看来我们无法嵌套thenApply()。有什么办法可以做到这一点?通过Google搜索它,发现了一个有用的链接- http://kennethjorgensen.com/blog/2016 / introduction-to-completablefutures ,但并没有太大帮助。

I am trying to pass result of addition to subtraction to multiplication to printing result. But I am getting compilation error. Looks like we can't do nested thenApply(). Is there any way we can do this? Searched it over google and found one helpful link- http://kennethjorgensen.com/blog/2016/introduction-to-completablefutures But didn't find much help.

推荐答案

有几处错误您的代码段:

A couple of things are wrong with your snippet:


  1. 肢体:您必须启动下一个 thenApply 之前的结尾,而不是在减去方法之后。

  2. supplyAsync( )是静态方法。

  3. 如果只想在最后一个操作中打印出结果,请使用 thenAccept 而不是 thenApply

  4. 您无需在 thenAccept 中完成CF(您都不会可以在 thenApply 之前完成。

  1. Parenthesis: you have to start the next thenApply after the end of the one before, not after the substract method.
  2. supplyAsync() is a static method. Use it as such.
  3. If you just want to print out the result in the last operation, use thenAccept instead of thenApply
  4. You do not need to complete the CF in thenAccept (neither you would have to do it in thenApply before.

这段代码可以编译,并且它可能接近您想要实现的目标:

This piece of code compiles, and it may be close to what you want to achieve:

    CompletableFuture<Void> cf = CompletableFuture
        .supplyAsync(() -> addition(a, b))
        .thenApply(r -> subtract(20, r))
        .thenApply(r1 -> multiply(r1, 10))
        .thenApply(r2 -> convert(r2))
        .thenAccept(finalResult -> {
            System.out.println("this is the final result: " + finalResult);
        });

    //just to wait until the cf is completed - do not use it on your program
    cf.join();

这篇关于然后将其应用于一个compleableFuture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆