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(); 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); } }
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(); 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); } }
JAVA多线程异步编排CompletableFuture线程串行化多任务组合------JAVA
最新推荐文章于 2024-11-04 20:33:37 发布