JAVA多线程异步编排CompletableFuture线程串行化API------JAVA

package com.alatus.search;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        CompletableFuture会启动一个异步任务,当它用的是supplyAsync的时候,是可以返回值的
//        我们可以和FutureTask一样通过get来获取这个值
        CompletableFuture.runAsync(() -> {
            System.out.println("异步任务");
        },service);
//        CompletableFuture<Long> longCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            long id = Thread.currentThread().getId();
//            System.out.println(id);
//            int i = 10 / 0;
//            return id;
//        }, service).whenComplete((result,exception)->{
            可以对结果进行获取和异步编排,并输出存在或出现的异常
            但是没法修改返回数据
//            System.out.println("结果是:"+result+"异常是:"+exception);
//        }).exceptionally(throwable -> {
            出现异常的默认返回
            可以修改返回数据
//            return 10L;
//        });
        CompletableFuture<Long> longCompletableFuture = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).handle((result,exception)->{
//            方法完成后的处理,无论是成功完成还是失败完成
            if(result!=null){
                return result;
            }
            if(exception!=null){
                System.out.println(exception);
                return 0L;
            }
            return 0L;
        });
        Long l = longCompletableFuture.get();
        System.out.println(l);
        System.out.println("我是main方法");
//        串行化任务
//        TODO 开启二号线程继续跑的方式,但是接收不到返回值
//        TODO 也用不了上一次的返回值
        CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).thenRunAsync(()->{
            System.out.println("第二个线程启动了");
        },service);
//        TODO 能接收到结果,但是返回值就没了
        CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).thenAcceptAsync(resp->{
            System.out.println("结果是"+resp);
        });
        // TODO 既能接收上一部结果,也有返回值
        CompletableFuture<Long> longCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).thenApplyAsync(resp -> {
            System.out.println("结果是" + resp++);
            return resp;
        });
        // TODO 两个异步任务合并,两个都完成
        CompletableFuture<Long> completableFuture01 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            return id;
        });
        CompletableFuture<Long> completableFuture02 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(id);
            return id;
        });
        CompletableFuture<Long> completableFuture03 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            return id;
        });
        completableFuture01.runAfterBothAsync(completableFuture02,()->{
            System.out.println("任务3开始");
        },service).runAfterBothAsync(completableFuture03,()->{
            System.out.println("任务都完成了");
        },service);
//        这里的串行执行使用的都是线程池给到的一个线程,所有的都是这个线程做的
//        但是执行完的值是不能返回再次使用的
        completableFuture01.thenAcceptBothAsync(completableFuture02,(f1,f2)->{
            System.out.println(f1);
            System.out.println(f2);
            System.out.println(f1+f2);
        },service).thenAcceptBothAsync(completableFuture03,(f1,f2)->{
            System.out.println(f2);
            System.out.println(f2);
        },service);
//        组合执行两个任务,并把值保留下来返回
        completableFuture01.thenCombineAsync(completableFuture02,(f1,f2)->{
            return f1+f2;
        },service).thenAcceptBothAsync(completableFuture03,(f1,f2)->{
//            在这里就可以把之前的值拿来用了
            System.out.println(f1+f2);
        },service);
//        两个任务只要有一个完成,我们就执行任务
//        不感知结果,自己也没有返回值
        completableFuture01.runAfterEitherAsync(completableFuture02,()->{
            System.out.println("两个任务只要一个完成,我就输出");
        },service);
//        感知结果的方式
        completableFuture01.acceptEitherAsync(completableFuture02,resp->resp++,service).runAfterEitherAsync(completableFuture03,()->{
            System.out.println("哈哈");
        },service);
    }
}
package com.alatus.search;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        CompletableFuture会启动一个异步任务,当它用的是supplyAsync的时候,是可以返回值的
//        我们可以和FutureTask一样通过get来获取这个值
        CompletableFuture.runAsync(() -> {
            System.out.println("异步任务");
        },service);
//        CompletableFuture<Long> longCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            long id = Thread.currentThread().getId();
//            System.out.println(id);
//            int i = 10 / 0;
//            return id;
//        }, service).whenComplete((result,exception)->{
            可以对结果进行获取和异步编排,并输出存在或出现的异常
            但是没法修改返回数据
//            System.out.println("结果是:"+result+"异常是:"+exception);
//        }).exceptionally(throwable -> {
            出现异常的默认返回
            可以修改返回数据
//            return 10L;
//        });
        CompletableFuture<Long> longCompletableFuture = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).handle((result,exception)->{
//            方法完成后的处理,无论是成功完成还是失败完成
            if(result!=null){
                return result;
            }
            if(exception!=null){
                System.out.println(exception);
                return 0L;
            }
            return 0L;
        });
        Long l = longCompletableFuture.get();
        System.out.println(l);
        System.out.println("我是main方法");
//        串行化任务
//        TODO 开启二号线程继续跑的方式,但是接收不到返回值
//        TODO 也用不了上一次的返回值
        CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).thenRunAsync(()->{
            System.out.println("第二个线程启动了");
        },service);
//        TODO 能接收到结果,但是返回值就没了
        CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).thenAcceptAsync(resp->{
            System.out.println("结果是"+resp);
        });
        // TODO 既能接收上一部结果,也有返回值
        CompletableFuture<Long> longCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            int i = 10 / 4;
            return id;
        }, service).thenApplyAsync(resp -> {
            System.out.println("结果是" + resp++);
            return resp;
        });
        // TODO 两个异步任务合并,两个都完成
        CompletableFuture<Long> completableFuture01 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            return id;
        });
        CompletableFuture<Long> completableFuture02 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(id);
            return id;
        });
        CompletableFuture<Long> completableFuture03 = CompletableFuture.supplyAsync(() -> {
            long id = Thread.currentThread().getId();
            System.out.println(id);
            return id;
        });
        completableFuture01.runAfterBothAsync(completableFuture02,()->{
            System.out.println("任务3开始");
        },service).runAfterBothAsync(completableFuture03,()->{
            System.out.println("任务都完成了");
        },service);
//        这里的串行执行使用的都是线程池给到的一个线程,所有的都是这个线程做的
//        但是执行完的值是不能返回再次使用的
        completableFuture01.thenAcceptBothAsync(completableFuture02,(f1,f2)->{
            System.out.println(f1);
            System.out.println(f2);
            System.out.println(f1+f2);
        },service).thenAcceptBothAsync(completableFuture03,(f1,f2)->{
            System.out.println(f2);
            System.out.println(f2);
        },service);
//        组合执行两个任务,并把值保留下来返回
        completableFuture01.thenCombineAsync(completableFuture02,(f1,f2)->{
            return f1+f2;
        },service).thenAcceptBothAsync(completableFuture03,(f1,f2)->{
//            在这里就可以把之前的值拿来用了
            System.out.println(f1+f2);
        },service);
//        两个任务只要有一个完成,我们就执行任务
//        不感知结果,自己也没有返回值
        completableFuture01.runAfterEitherAsync(completableFuture02,()->{
            System.out.println("两个任务只要一个完成,我就输出");
        },service);
//        感知结果的方式
        completableFuture01.acceptEitherAsync(completableFuture02,resp->resp++,service).runAfterEitherAsync(completableFuture03,()->{
            System.out.println("哈哈");
        },service);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值