一、根据阶段正常完成结果的产出型(或者叫函数型):
这一类方法都由上一阶段(或者两个阶段,或者两个阶段中的任意一个)的正常完成结果触发,然后以该结果执行给定的函数,产出新的结果。这里把异步执行的两者形式也列举出来了。
//依赖单个阶段
public <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn); // 默认执行方式
public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn);// 默认的异步执行方式
public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn,Executor executor); //自定义的执行方式
//依赖两个阶段都完成
public <U,V> CompletionStage<V> thenCombine(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn);
public <U,V> CompletionStage<V> thenCombineAsync(CompletionStage<? extends U> other, BiFunction<? super T,? super U,? extends V> fn, Executor executor);
//依赖两个阶段中的任何一个完成
public <U> CompletionStage<U> applyToEither(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn);
public <U> CompletionStage<U> applyToEitherAsync(CompletionStage<? extends T> other,Function<? super T, U> fn,Executor executor);
代码如下:
Person类
package com.redisson;
/**
* @Description TODO
* @Date 2020/7/28 13:41
* @Author zsj
*/
public class Person {
public int age;
public String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试类如下:
package com.redisson;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
/**
* @Description TODO
* @Date 2020/7/28 9:09
* @Author zsj
*/
public class MainFour {
public static void main(String[] args) throws Exception {
// thenCombineAsync();
// thenApplyAsync();
applyToEitherAsync();
Thread.sleep(10000);
}
public static void thenCombineAsync() {
CompletableFuture<Person> one = CompletableFuture.supplyAsync(new Supplier<Person>() {
@Override
public Person get() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Person p = new Person();
p.setAge(10);
p.setName("p1");
return p;
}
});
CompletableFuture<Person> two = CompletableFuture.supplyAsync(new Supplier<Person>() {
@Override
public Person get() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Person p = new Person();
p.setAge(20);
p.setName("p2");
return p;
}
});
CompletableFuture<Person> three = one.thenCombineAsync(two, (p1, p2) -> {
Person p3 = new Person();
p3.setAge(p1.getAge());
p3.setName(p2.getName());
return p3;
});
three.thenAcceptAsync((person) -> {
System.out.println(person.getAge());
System.out.println(person.getName());
}).exceptionally((ee) -> {
ee.printStackTrace();
return null;
});
}
public static void thenApplyAsync() {
CompletableFuture<Person> one = CompletableFuture.supplyAsync(new Supplier<Person>() {
@Override
public Person get() {
t