zoukankan      html  css  js  c++  java
  • CompletableFuture 测试类

    package com.example.apidemo.completableFutrue;
    
    import java.util.Random;
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.TimeUnit;
    import java.util.function.BiConsumer;
    import java.util.function.BiFunction;
    import java.util.function.Function;
    import java.util.function.Supplier;
    
    /**
     * @Author Tim
     * @Date 2021/10/25 14:18
     */
    public class TestComplete {
    
        //runAsync方法不支持返回值。
        //supplyAsync可以支持返回值。
        public static void main1(String[] args) throws Exception {
    
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                try {
                    System.out.println("run end1 ..." + Thread.currentThread().getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("run end2 ..." + Thread.currentThread().getName());
                return "返回值:" + System.currentTimeMillis();
            });
            System.out.println("run end3 ...:" + future.get() + "/" + Thread.currentThread().getName());
        }
    
    
        //当CompletableFuture的计算结果完成的回调方法
        public static void main2(String[] args) throws Exception {
    
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                try {
                    System.out.println("run ..." + Thread.currentThread().getName());
                } catch (Exception e) {
                }
                System.out.println("run end ..." + Thread.currentThread().getName());
                return "1234567";
            }).whenComplete((String v, Throwable t) -> {
                System.out.println("run end2 ..." + v + "/" + Thread.currentThread().getName() + "/" + t);
            });
            // v是有返回值的回调的结果,t 是线程... 可省略写法:whenComplete((v, t) -> {});
        }
    
    
        //thenApply 方法 : 当一个线程依赖另一个线程时,可以使用 thenApply 方法来把这两个线程串行化。
        //.thenApply(Function<? super T, ? extends U> fn) T是上一个任务返回结果的类型。U:当前任务的返回值类型
        public static void main3(String[] args) throws Exception {
            CompletableFuture<Long> future = CompletableFuture.supplyAsync(new Supplier<Long>() {
                @Override
                public Long get() {
                    long result = new Random().nextInt(100);
                    System.out.println("result1 ="+result);
                    return result;
                }
            }).thenApply(new Function<Long, Long>() {
                @Override
                public Long apply(Long t) {
                    long result = t*5;
                    System.out.println("result2 ="+result);
                    return result;
                }
            });
            long result = future.get();
            System.out.println("result--------:" + result);
        }
    
    
        //handle 是执行任务(包括出现异常)完成时对结果的处理。 而thenApply 方法,如果上个任务出现错误,则不会执行 thenApply 方法。
        public static void main4(String[] args) throws Exception {
            CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
                @Override
                public Integer get() {
                    return new Random().nextInt(10);
                }
            }).handle(new BiFunction<Integer, Throwable, Integer>() {
                @Override
                public Integer apply(Integer param, Throwable throwable) {
                    int result = 1;
                    if (throwable == null){
                        result = param * 2;
                        System.out.println("apply: ==== " + param);
                    } else {
                        System.out.println("error: ==== " + throwable.getMessage());
                    }
                    return result;
                }
            });
            System.out.println("result--------:" + future.get());
        }
    
    
        //thenAccept 接收任务的处理结果,并消费处理,无返回结果。没有后续的输出操作, 所以future.get() 是null
        public static void main5(String[] args) throws ExecutionException, InterruptedException {
            CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
                @Override
                public Integer get() {
                    return new Random().nextInt(10);
                }
            }).thenAccept(integer -> {
                System.out.println("integer====" + integer);
            });
            System.out.println("result--------:" + future.get());
        }
    
    
        // thenRun: 该方法同 thenAccept 方法类似。不同的是上个任务处理完成后,并不会把计算的结果传给 thenRun 方法。
        // 只是处理玩任务后,执行 thenAccept 的后续操作
        public static void main6(String[] args) throws ExecutionException, InterruptedException {
            CompletableFuture<Void> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
                @Override
                public Integer get() {
                    return new Random().nextInt(10);
                }
            }).thenRun(() -> {
                System.out.println("thenRun ..." + 1);
            });
            System.out.println("result--------:" + future.get());
        }
    
    
        //thenCombine 合并任务
        public static void main(String[] args) throws Exception {
            CompletableFuture<String> future1 = CompletableFuture.supplyAsync(new Supplier<String>() {
                @Override
                public String get() {
                    return "zhangshan";
                }
            });
            CompletableFuture<String> future2 = CompletableFuture.supplyAsync(new Supplier<String>() {
                @Override
                public String get() {
                    return "lisi";
                }
            });
            CompletableFuture<String> result = future1.thenCombine(future2, new BiFunction<String, String, String>() {
                @Override
                public String apply(String t, String u) {
                    return t + "===" + u;
                }
            });
            System.out.println("result--------:" + result.get());
        }
    
    }
    路在脚下
  • 相关阅读:
    解决Extjs分页工具条Ext.PagingToolbar无法换页问题 子曰
    使用“动软代码生成器”需要注意的问题 子曰
    格式化extjs DateField 的值 子曰
    构造extjs两级联动comBox 子曰
    SQLServer数据库设计表和字段(转) 子曰
    extjs 中取值的方式 子曰
    extjs中的控件无法正常显示 子曰
    extjs表单中的下拉框(comobobox)手动添加空选项 子曰
    C++基础学习笔记
    dhl:弹出div层,可关闭可移动
  • 原文地址:https://www.cnblogs.com/lgg20/p/15459362.html
Copyright © 2011-2022 走看看