03-CompletableFuture系列-异步任务,聚合关系

本节主要介绍异步任务并行执行以及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聚合关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值