加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何访问传递给CompletableFuture allOf的已完成期货?

发布时间:2020-05-25 15:16:49 所属栏目:Java 来源:互联网
导读:我试图掌握 Java 8 CompletableFuture.我怎样才能将这些人加入到“allOf”之后再归还给他们.下面的代码不起作用,但让你知道我尝试过的. 在javascript ES6中我会这样做 Promise.all([p1, p2]).then(function(persons) { console.log(persons[0]); // p1 return

我试图掌握 Java 8 CompletableFuture.我怎样才能将这些人加入到“allOf”之后再归还给他们.下面的代码不起作用,但让你知道我尝试过的.

在javascript ES6中我会这样做

Promise.all([p1,p2]).then(function(persons) {
   console.log(persons[0]); // p1 return value     
   console.log(persons[1]); // p2 return value     
});

到目前为止我在Java方面的努力

public class Person {

        private final String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

    }

@Test
public void combinePersons() throws ExecutionException,InterruptedException {
    CompletableFuture<Person> p1 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    CompletableFuture<Person> p2 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    CompletableFuture.allOf(p1,p2).thenAccept(it -> System.out.println(it));

}

解决方法

CompletableFuture#allOf方法不公开传递给它的已完成CompletableFuture实例的集合.

Returns a new CompletableFuture that is completed when all of the
given CompletableFutures complete. If any of the given
CompletableFutures complete exceptionally,then the returned
CompletableFuture also does so,with a CompletionException holding
this exception as its cause. Otherwise,the results,if any,of the
given CompletableFutures are not reflected in the returned
CompletableFuture,but may be obtained by inspecting them
individually. If no CompletableFutures are provided,returns a
CompletableFuture completed with the value null.

请注意,allOf还会考虑已完成的特殊期货.所以你不会总是有一个人可以使用.你可能实际上有一个异常/ throwable.

如果您知道正在使用的CompletableFutures的数量,请直接使用它们

CompletableFuture.allOf(p1,p2).thenAccept(it -> {
    Person person1 = p1.join();
    Person person2 = p2.join();
});

如果你不知道你有多少(你正在使用数组或列表),只需捕获传递给allOf的数组

// make sure not to change the contents of this array
CompletableFuture<Person>[] persons = new CompletableFuture[] { p1,p2 };
CompletableFuture.allOf(persons).thenAccept(ignore -> {
   for (int i = 0; i < persons.length; i++ ) {
       Person current = persons[i].join();
   }
});

如果你想要你的combinePersons方法(现在忽略它是@Test)来返回包含完成的期货中所有Person对象的Person [],你可以做

@Test
public Person[] combinePersons() throws Exception {
    CompletableFuture<Person> p1 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    CompletableFuture<Person> p2 = CompletableFuture.supplyAsync(() -> {
        return new Person("p1");
    });

    // make sure not to change the contents of this array
    CompletableFuture<Person>[] persons = new CompletableFuture[] { p1,p2 };
    // this will throw an exception if any of the futures complete exceptionally
    CompletableFuture.allOf(persons).join();

    return Arrays.stream(persons).map(CompletableFuture::join).toArray(Person[]::new);
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读