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()); } }
学海无涯 代码作伴