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; }); System.out.println(longCompletableFuture1.get()); } }
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; }); System.out.println(longCompletableFuture1.get()); } }
JAVA多线程异步编排CompletableFuture线程串行化执行------JAVA
最新推荐文章于 2024-11-04 20:33:37 发布