本节主要介绍异步任务并行执行以及AND聚合执行
异步任务并行执行其实就是创建两个异步任务。
举个简单例子:获取用户信息和获取书籍信息可以并行执行,无依赖关系
public class CompletableFutureDemo {
public static void main(String[] args) throws InterruptedException {
//获取用户信息
CompletableFuture<User> future1 = CompletableFuture.supplyAsync(() -> {
int userId = 1;
return getUserById(userId);
});
//获取书籍信息
CompletableFuture<Book> future2 = CompletableFuture.supplyAsync(() -> {
int bookId = 1;
return getBookById(bookId);
});
}
public static User getUserById(int id) {
User user = new User(1, "Lindsay98");
if (id == user.getId()) {
return user;
} else {
return null;
}
}
public static Book getBookById(int id) {
Book book = new Book(1, "围城");
if (id == book.getId()) {
return book;
} else {
return null;
}
}
}
描述AND聚合关系
thenCombine、thenAcceptBoth、runAfterBoth
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<Void> thenAcceptBoth(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action);
public <U> CompletionStage<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,BiConsumer<? super T, ? super U> action,Executor executor);
public CompletionStage<Void> runAfterBoth(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action);
public CompletionStage<Void> runAfterBothAsync(CompletionStage<?> other,Runnable action,Executor executor);
这些接口的区别也是源自核心参数fn、consumer、action及Async和线程池。
在第二节中已经介绍过了。
02-CompletableFuture系列-异步任务串行执行
举例:
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
public class CompletableFutureDemo {
public static void main(String[] args) throws InterruptedException {
//获取用户信息
CompletableFuture<User> future1 = CompletableFuture.supplyAsync(() -> {
int userId = 1;
return getUserById(userId);
});
//获取书籍信息
CompletableFuture<Book> future2 = CompletableFuture.supplyAsync(() -> {
int bookId = 1;
return getBookById(bookId);
});
//用户收藏的书
Map<User, List<Book>> userLikeBooks = new HashMap<>();
//用户信息和书籍信息都获取完成后汇聚
future1.thenAcceptBoth(future2,(f1,f2) -> {
userLikeBooks.put(f1, Lists.newArrayList(f2));
});
System.out.println(userLikeBooks);
}
public static User getUserById(int id) {
User user = new User(1, "Lindsay98");
if (id == user.getId()) {
return user;
} else {
return null;
}
}
public static Book getBookById(int id) {
Book book = new Book(1, "围城");
if (id == book.getId()) {
return book;
} else {
return null;
}
}
}
下一节:描述CompletableFutureOR聚合关系