CompletableFuture-对计算结果进行处理

CompletableFuture-对计算结果进行处理_第1张图片

package com.nanjing.gulimall.zhouyimo.test;

import java.util.concurrent.*;

/**
 * @author zhou
 */
public class CompletableFutureAPI2Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("111");
            return 1;
        }, threadPool).thenApply(f -> {
            System.out.println("222");
            return f + 2;
        }).thenApply(f -> {
            System.out.println("3333");
            return f + 3;
        }).whenComplete((v,e) ->{
            if(e == null){
                System.out.println("-----计算结果: "+v);
        }}).exceptionally(e -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });
        System.out.println(Thread.currentThread().getName() + "------主线程先去做其他事情");
        threadPool.shutdown();
    }
}


main------主线程先去做其他事情
222
3333
-----计算结果: 6

加了int i = 10/0;模拟一个异常

package com.nanjing.gulimall.zhouyimo.test;

import java.util.concurrent.*;

/**
 * @author zhou
 */
public class CompletableFutureAPI2Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("111");
            return 1;
        }, threadPool).thenApply(f -> {
            int i = 10/0;
            System.out.println("222");
            return f + 2;
        }).thenApply(f -> {
            System.out.println("3333");
            return f + 3;
        }).whenComplete((v,e) ->{
            if(e == null){
                System.out.println("-----计算结果: "+v);
        }}).exceptionally(e -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });
        System.out.println(Thread.currentThread().getName() + "------主线程先去做其他事情");
        threadPool.shutdown();
    }
}


main------主线程先去做其他事情
111
java.lang.ArithmeticException: / by zero
java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
	at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
	at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
	at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:618)
	at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:591)
	at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:488)
	at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1609)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:750)
Caused by: java.lang.ArithmeticException: / by zero
	at com.nanjing.gulimall.zhouyimo.test.CompletableFutureAPI2Demo.lambda$main$1(CompletableFutureAPI2Demo.java:20)
	at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:616)
	... 6 more

演示handle

package com.nanjing.gulimall.zhouyimo.test;

import java.util.concurrent.*;

/**
 * @author zhou
 */
public class CompletableFutureAPI2Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("111");
            return 1;
        }, threadPool).handle((f,e) -> {
            int i = 10/0;
            System.out.println("222");
            return f + 2;
        }).handle((f,e) -> {
            System.out.println("3333");
            return f + 3;
        }).whenComplete((v,e) ->{
            if(e == null){
                System.out.println("-----计算结果: "+v);
        }}).exceptionally(e -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });
        System.out.println(Thread.currentThread().getName() + "------主线程先去做其他事情");
        threadPool.shutdown();
    }
}


main------主线程先去做其他事情
111
3333
java.lang.NullPointerException
java.util.concurrent.CompletionException: java.lang.NullPointerException
	at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273)
	at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280)
	at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:838)
	at java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:811)
	at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:488)
	at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1609)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:750)
Caused by: java.lang.NullPointerException
	at com.nanjing.gulimall.zhouyimo.test.CompletableFutureAPI2Demo.lambda$main$2(CompletableFutureAPI2Demo.java:25)
	at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:836)
	... 6 more

 

 

 

 

你可能感兴趣的:(JUC并发编程与源码分析,java,开发语言)