• CompletableFuture异步编排


    代码一

    public class ThreadTest {
    
    	public static ExecutorService executor = Executors.newFixedThreadPool(10);
    	
    	public static void main(String[] args) throws Exception {
    		System.out.println("main---start");
    //		CompletableFuture.runAsync(()->{
    //			System.out.printf("当前线程  %d 
    ",Thread.currentThread().getId());
    //			int i = 10/2;
    //			System.out.printf("运行结果:%d",i);
    //		}, executor);
    		CompletableFuture<Integer> future = CompletableFuture.supplyAsync(()->{
    			System.out.printf("当前线程  %d 
    ",Thread.currentThread().getId());
    			int i = 10/0;
    			System.out.printf("运行结果:%d 
    ",i);
    			return i;
    		}, executor).whenComplete((result,exception)->{
    			//能得到异常信息,没法修改返回数据
    			System.out.println("异步任务成功完成了...
    结果是 " + result + "
    异常是:" + exception);
    		}).exceptionally(throwable -> {
    			//可以感知异常,同时返回默认值
    			return 777;
    		});
    		Integer result = future.get();
    		System.out.printf("main---end... 结果是:%d ",result);
    	}	
    }
    

    运行结果

    image

    代码二

    public class ThreadTest {
    
    	public static ExecutorService executor = Executors.newFixedThreadPool(10);
    	
    	public static void main(String[] args) throws Exception {
    		System.out.println("main---start");
    		CompletableFuture<Integer> future = CompletableFuture.supplyAsync(()->{
    			System.out.printf("当前线程  %d 
    ",Thread.currentThread().getId());
    			int i = 10/0;
    			System.out.printf("运行结果:%d 
    ",i);
    			return i;
    			//handle比whenComplete更强大,可以处理结果
    		}, executor).handle((result,exception)->{
    			if(result != null) {
    				return result*2;
    			}
    			if(exception != null) {
    				return 1990;
    			}
    			return -1;
    		});
    		Integer result = future.get();
    		System.out.printf("main---end... 结果是:%d ",result);
    	}	
    }
    

    运行结果

    image

    代码三

    public class ThreadDemo {
    	
    	public static ExecutorService executor = Executors.newFixedThreadPool(10);
    
    	public static void main(String[] args) throws Exception{
    		System.out.println("main...start...");
    		/**
    		 * 线程串行化
    		 * 1、thenRunAsync:不能获取上一步的执行结果,无返回值
    		 * 
    		 CompletableFuture.supplyAsync(() -> {
    			System.out.println("当前线程:" + Thread.currentThread().getId());
    			int i = 10 / 2;
    			System.out.println("运行结果:" + i);
    			return i;
    		}, executor).thenRunAsync(() -> {
    			System.out.println("任务2启动了");
    		}, executor);
    		
    		   2、thenAcceptAsync:能接收上一步的结果,但是无返回值
    		   
    		   CompletableFuture.supplyAsync(() -> {
    			System.out.println("当前线程:" + Thread.currentThread().getId());
    			int i = 10 / 2;
    			System.out.println("运行结果:" + i);
    			return i;
    		}, executor).thenAcceptAsync(res -> {
    			System.out.println("任务2启动了 " + res);
    		}, executor);
    		
    			3、thenApplyAsync:能接收上一步的结果,有返回值
    			
    			thenApplyAsync(res -> {
    			System.out.println("任务2启动了 " + res);
    			return res * 3;
    		}, executor)
    		 */
    		CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
    			System.out.println("当前线程:" + Thread.currentThread().getId());
    			int i = 10 / 2;
    			System.out.println("运行结果:" + i);
    			return i;
    		}, executor).thenApplyAsync(res -> {
    			System.out.println("任务2启动了 " + res);
    			return res * 3;
    		}, executor);
    		System.out.println("main...end..." + future.get());
    	}
    }
    

    运行结果

    image

    代码四

    public class ThreadDemo {
    	
    	public static ExecutorService executor = Executors.newFixedThreadPool(10);
    
    	public static void main(String[] args) throws Exception{
    		System.out.println("main...start...");
    		
    		/**
    		 * 两个都完成
    		 */
    		CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
    			System.out.println("任务1线程:" + Thread.currentThread().getId());
    			int i = 10 / 2;
    			System.out.println("任务1结束");
    			return i;
    		}, executor);
    		
    		CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
    			System.out.println("任务2线程:" + Thread.currentThread().getId());
    			System.out.println("任务2结束");
    			return "Hello";
    		}, executor);
    		
    		//无返回结果,取不到结果
    //		future01.runAfterBothAsync(future02, () -> {
    //			System.out.println("任务3开始...");
    //		}, executor);
    		
    		//无返回结果,可以取到结果,但不能返回
    //		future01.thenAcceptBothAsync(future02, (f1,f2) -> {
    //			System.out.println("任务3开始。。。汇总了任务1和2的结果:" + f2 + " " + f1);
    //		}, executor);
    		
    		//有返回结果,可以取到结果,可以返回
    		CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1,f2) -> {
    			return f2 + f1 + " 你好...";
    		}, executor);
    		System.out.println("main...end..." + future.get());
    	}
    }
    

    运行结果

    image

    代码五

    public class ThreadDemo {
    	
    	public static ExecutorService executor = Executors.newFixedThreadPool(10);
    
    	public static void main(String[] args) throws Exception{
    		System.out.println("main...start...");
    		
    		CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
    			System.out.println("任务1线程:" + Thread.currentThread().getId());
    			int i = 10 / 2;
    			System.out.println("任务1结束");
    			return i;
    		}, executor);
    		
    		CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
    			System.out.println("任务2线程:" + Thread.currentThread().getId());
    			try {
    				TimeUnit.SECONDS.sleep(3);
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			System.out.println("任务2结束");
    			return "Hello";
    		}, executor);
    		
    		/**
    		 * 两个都完成只要有一个完成,就执行任务3
    		 * runAfterEitherAsync :不感知结果,无返回值
    		 * acceptEitherAsync :感知结果,有返回值
    		 */
    //		future01.runAfterEitherAsync(future02, ()->{
    //			System.out.println("任务3开始");
    //		},executor);
    //		future01.acceptEitherAsync(future02, res -> {
    //			System.out.println("任务3 " + res);
    //		}, executor);
    		CompletableFuture<String> future = future01.applyToEitherAsync(future02, (res) -> {
    			return "fuck " + res;
    		}, executor);
    		
    		System.out.println("main...end..." + future.get());
    	}
    }
    

    运行结果

    image

    代码六

    public class ThreadDemo {
    	
    	public static ExecutorService executor = Executors.newFixedThreadPool(10);
    
    	public static void main(String[] args) throws Exception{
    		System.out.println("main...start...");
    		CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
    			System.out.println("查询商品的图片信息");
    			return "hello.jpg";
    		}, executor);
    		CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
    			try {
    				TimeUnit.SECONDS.sleep(3);
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			System.out.println("查询商品的属性");
    			return "黑色  + 256G";
    		}, executor);
    		CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
    			System.out.println("查询商品介绍");
    			return "华为";
    		}, executor);
    		
    		//必须之前所有任务都成功
    //		CompletableFuture<Void> future = CompletableFuture.allOf(futureImg,futureAttr,futureDesc);
    		//只要有一个成功就可以
    		CompletableFuture<Object> future = CompletableFuture.anyOf(futureImg,futureAttr,futureDesc);
    		//等待所有任务完成
    		future.get();
    //		System.out.println("main...end..." + futureImg.get() + "_" + futureAttr.get() + "_" + futureDesc.get());
    		System.out.println("main...end...结果:" + future.get());
    	}
    }
    

    运行结果

    image

  • 相关阅读:
    宿舍助手app——个人工作第四天
    宿舍助手app——个人工作第三天
    对QQ输入法的评价
    冲刺9
    冲刺8
    冲刺7
    冲刺6
    冲刺5
    冲刺4
    冲刺3
  • 原文地址:https://www.cnblogs.com/kaka-qiqi/p/15178188.html
Copyright © 2020-2023  润新知