Java CompletableFuture
实现了 CompletionStage
和 Future
接口。 CompletableFuture.applyToEither
继承自 CompletionStage
。 applyToEither
方法返回一个新的 CompletionStage
,当这个或另一个给定阶段正常完成时,将使用相应的结果作为所提供函数的参数执行。
从 Java 文档中找到 applyToEither
方法的方法声明。
<U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other, Function<? super T,U> fn)
类型参数U是函数的返回类型
参数other 是另一个CompletionStage
。
参数 fn 是用于计算返回的 CompletionStage
值的函数。applyToEither
返回新的 CompletionStage
。
查找示例。
示例 1:
ApplyToEitherDemo1.java
package com.concretepage;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class ApplyToEitherDemo1 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
CompletableFuture<Person> primaryFuture = CompletableFuture.completedFuture(new Person("Mohan", "Varanasi"));
CompletableFuture<Person> secondaryFuture = CompletableFuture.completedFuture(new Person("Shyam", "Prayagraj"));
CompletableFuture<String> future =
primaryFuture.applyToEither(secondaryFuture, person -> person.getName() + " - " +person.getCity());
System.out.println(future.get());
}
}
输出
Mohan - Varanasi
applyToEither
方法将此完成阶段的结果或其他通常较早完成的完成阶段的结果应用于给定函数并返回一个新的完成阶段。在上面的例子中,有时我们会得到 primaryFuture
的结果,有时会得到 secondaryFuture
的结果。
示例 2:
ApplyToEitherDemo2.java
package com.concretepage;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class ApplyToEitherDemo2 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
CompletableFuture<Person> mainFuture = CompletableFuture.supplyAsync(() -> getPerson());
CompletableFuture<Person> defaultFuture = CompletableFuture.supplyAsync(() -> getDefaultFuture());
CompletableFuture<String> future = mainFuture.applyToEither(defaultFuture,
person -> person.getName() + " - " + person.getCity());
System.out.println(future.join());
}
private static Person getPerson() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Person("Krishna", "Delhi");
}
private static Person getDefaultFuture() {
return new Person("Default name", "Default city");
}
}
输出
Default name - Default city
这里 defaultFuture
将比 mainFuture
更早完成。所以 applyToEither
会将 defaultFuture
的结果应用于给定的函数。
Example-3:使用 acceptEither
方法查找 applyToEither
的示例。
package com.concretepage;
import java.util.concurrent.CompletableFuture;
public class ApplyToEitherDemo3 {
public static void main(String[] args) {
CompletableFuture<Person> mainFuture = CompletableFuture.supplyAsync(() -> getPerson());
CompletableFuture<Person> defaultFuture = CompletableFuture.supplyAsync(() -> getDefaultFuture());
CompletableFuture<String> cfuture = mainFuture.applyToEither(defaultFuture,
person -> person.getName() + " - " + person.getCity());
CompletableFuture<String> otherCFuture = CompletableFuture.supplyAsync(() -> getMsg());
CompletableFuture<Void> cf = cfuture.acceptEither(otherCFuture, s -> System.out.println(s));
cf.join();
}
private static String getMsg() {
try {
Thread.sleep(400);
// Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println(e);
}
return "Namo - Gujraat";
}
private static Person getPerson() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Person("Krishna", "Delhi");
}
private static Person getDefaultFuture() {
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new Person("Default name", "Default city");
}
}
输出
Namo - Gujraat
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.concretepage.com/java/java-8/java-completablefuture-applytoeither
内容来源于网络,如有侵权,请联系作者删除!