首页 > 其他分享 >70、商城业务---异步---CompletableFuture---多任务组合

70、商城业务---异步---CompletableFuture---多任务组合

时间:2023-02-07 19:25:29浏览次数:49  
标签:return service System --- CompletableFuture 70 println out

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

public class ThreadTest {
    public static ExecutorService service = Executors.newFixedThreadPool(5);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的信息");
            return "商品";
        }, service);

        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询属性");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "属性";
        }, service);

        CompletableFuture<String> future03 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询图片信息");
            return "图片";
        }, service);

        /**
         * 等待全部运行完毕
         * 第一种:阻塞式等待,这样会等待执行时间最长的线程  【不推荐】
         */
//        future01.get();
//        future02.get();
//        future03.get();

        /**
         * 等待全部运行完毕
         * 第二种:使用allof()方法等待所有线程运行完毕  【推荐】
         */
//        CompletableFuture<Void> future = CompletableFuture.allOf(future01, future02, future03);
//        future.get();//等待所有线程运行完毕

//        ====================================================================

        /**
         * 等待其中一个运行完毕
         */
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future01, future02, future03);
        anyOf.get();
        System.out.println("运行结果::" + anyOf.get());

        System.out.println("全部线程运行完毕");
    }

    public static void test(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main-----start");

        //thenRunAsync
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程::" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果::" + i);

            return i;
        }, service).thenRunAsync(()->{
            System.out.println("任务二启动了");
        },service);

        //thenAcceptAsync
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程::" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果::" + i);

            return i;
        }, service).thenAcceptAsync((result) -> {
            System.out.println("上一步的执行结果::" + result);
        }, service);

        //thenApplyAsync
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程::" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果::" + i);

            return i;
        }, service).thenApplyAsync((result) -> {
            return "hello---" + result;
        }, service);
        System.out.println(stringCompletableFuture.get());

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一开始");
            int i = 10 / 2;
            System.out.println("任务一结束");
            return i;
        }, service);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二开始");
            int i = 10 / 4;
            System.out.println("任务二结束");
            return i;
        }, service);

        //runAfterBoth:组合两个 future,不需要获取 future 的结果,只需两个 future 处理完任务后, 处理该任务。
//        future1.runAfterBothAsync(future2, ()->{
//            System.out.println("任务三开始");
//        }, service);

        //thenAcceptBoth:组合两个 future,获取两个 future 任务的返回结果,然后处理任务,没有 返回值
//        future1.thenAcceptBothAsync(future2, (res1, res2)->{
//            System.out.println("任务三开始");
//            System.out.println("任务一的返回值::" + res1);
//            System.out.println("任务二的返回值::" + res2);
//            System.out.println("任务三结束");
//        }, service);

        //thenCombine:组合两个 future,获取两个 future 的返回结果,并返回当前任务的返回值
        CompletableFuture<String> future3 = future1.thenCombineAsync(future2, (res1, res2) -> {
            return res1 + "---" + res2 + "---" + "hello";
        }, service);
        System.out.println(future3.get());


        System.out.println("main-----end");
    }
}

标签:return,service,System,---,CompletableFuture,70,println,out
From: https://www.cnblogs.com/morehair/p/17099530.html

相关文章