Java中异步处理CompletableFuture

Java中异步处理CompletableFuture

异步处理简单来说就是另起一个线程处理,使程序不至于阻塞到某一步,导致后续代码无法执行。
异步处理的方法有很多,懒的话直接 new Thread(() -> {}).start(); ,也不是不行。但是这样会让
自己显得很low,不能向别人装13。再进一步,其实可以用线程池来管理,但是各种线程之间的通信也
很麻烦,还要注意线程安全的问题。jdk1.8提供了CompletableFuture类,扩展了Future接口,可以帮
助我们简化异步处理。

一个无返回值的异步处理过程:

public static void main(String[] args) throws InterruptedException {
 	CompletableFuture.runAsync(() -> {
 		try {
			Thread.sleep(1000);
	    } catch (InterruptedException e) {
            e.printStackTrace();
	    }
		System.out.println(Thread.currentThread() + "-----"
+System.currentTimeMillis());
	});

	System.out.println(Thread.currentThread() + "-----"
+System.currentTimeMillis());
	// 保证主线程不早于异步线程结束
	Thread.sleep(10000);
}

打印结果:
Thread[main,5,main]-----1582874573581
Thread[ForkJoinPool.commonPool-worker-3,5,main]-----1582874574584

从结果看,异步方法中的延迟1000ms,并没有影响到主线程的运行。
主线程中的sleep(10000),主要是防止主线程过早结束,因为CompletableFuture默认使用的是
ForkJoinPool,此线程池创建的线程为守护线程,如果非守护线程全部结束,其也会自动结束。
上面演示的为没有返回值的异步,下面演示有返回值的异步:

 public static void main(String[] args) throws InterruptedException { 		  		
        CompletableFuture<Long> future = CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long l = System.currentTimeMillis();
        System.out.println(Thread.currentThread() + "-----" + l);
        // 返回当前时间 
        return l;
    });

        try {
            // 获取异步执行结果 
            System.out.println(future.get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread() + "-----" + System.currentTimeMillis());                   
        Thread.sleep(10000);
    }
    运行结果:
    Thread[ForkJoinPool.commonPool-worker-3,5,main]-----1582875356876
            1582875356876
    Thread[main,5,main]-----1582875356914

根据结果看,异步方法执行了,future.get()阻塞了,获取到结果后继续执行后续任务。
因为上面说了,CompletableFuture实现了Future接口,当然可以用Future的get方法获取结果,但是
我们都知道,Future的get方法是会阻塞的,这对于我们异步执行没什么作用啊。对了,因为
CompletableFuture的正确用法不是直接使用get阻塞来获取结果。

下面演示非阻塞获取异步结果:

   public static void main(String[] args) throws InterruptedException {
        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long l = System.currentTimeMillis();
        System.out.println(Thread.currentThread() + "-----" + l);
        return l;
    }).thenAccept(time -> {
        System.out.println("thenAccept: " + time);
    });
        System.out.println(Thread.currentThread() + "-----" + System.currentTimeMillis());
        Thread.sleep(10000);
    }

    运行结果:
    Thread[main,5,main]-----1582875824352
    Thread[ForkJoinPool.commonPool-worker-3,5,main]-----1582875825351
    thenAccept: 1582875825351

根据结果来看,主线程没有被阻塞,最终的异步执行的方法结果也取到了。


上面使用了链式调用的thenAccept(Consumer<? super T> action),这个方法接受一个函数式接口
Consumer,Consumer这个函数式接口表示接受一个参数,不返回值。其实就是回调了这个
Consumer,把上一步的结果作为参数传递进来。
上面只是CompletableFuture的一些小功能,下面演示多个CompletableFuture串行执行:

CompletableFuture<List<Integer>> task1 = CompletableFuture.supplyAsync(() -> {
        System.out.println("任务1启动,开始创建1000个随机的整数");
            List<Integer> list = new ArrayList<>();
            Random random = new Random();
            for (int i = 0; i < 1000; i++) {
                list.add(random.nextInt(10000));
            }
        System.out.println("任务1执行成功");
            return list;
        });
        task1.thenApply(list -> {
            System.out.println("任务2启动,将任务1中的1000个随机数排序,从大到小");
        list.sort((val1, val2) -> val2 - val1);
            System.out.println("任务2执行成功");
            return list;
        }).thenAccept(list -> {
            System.out.println("所有任务执行完成,结果是:" + list);
        });
	        System.out.println(Thread.currentThread());
	        Thread.sleep(10000);
        }

        运行结果:
            任务1启动,开始创建1000个随机的整数
            任务1执行成功
            Thread[main,5,main]
            任务2启动,将任务1中的1000个随机数排序,从大到小
            任务2执行成功

            所有任务执行完成,结果是:[9994, 9979, 9968, 9949, 9937, 9931, 9931, 9912, 9899 ...

多个异步任务之间串行执行,用起来很方便,而且CompletableFuture的链式调用方法都支持lambda
表达式,写起来更加简洁,流程也很清晰。
有串行执行当然就有并行执行,多个CompletableFuture并行执行:

public static void main(String[] args) throws InterruptedException {
        int[] array = new int[10000];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100000);
        }

        // 并行执行
        CompletableFuture<Long> task1 = CompletableFuture.supplyAsync(() -> bubbleSort(array));
        // 并行执行
        CompletableFuture<Long> task2 = CompletableFuture.supplyAsync(() -> insertionSort(array));

        // 使用最先完成的
        CompletableFuture.anyOf(task1, task2).thenAccept(obj -> {
            System.out.println("最终结果为:" + obj);
        });
        System.out.println(Thread.currentThread());
        Thread.sleep(10000);
    }
    // 冒泡排序,返回排序时间
    private static long bubbleSort(int[] array) {
        long begin = System.currentTimeMillis();
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        long time = System.currentTimeMillis() - begin;
        System.out.println("AsyncMain.bubbleSort " + time);
        return time;
    }
    // 插入排序,返回排序时间
    private static long insertionSort(int[] array) {
        long begin = System.currentTimeMillis();
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j = i;
            while (j > 0 && temp < array[j - 1]) {
                array[j] = array[j - 1];
                j--;
            }
            array[j] = temp;
        }
        long time = System.currentTimeMillis() - begin;
        System.out.println("AsyncMain.insertionSort " + time);
        return time;
    }
    运行结果为:
    Thread[main,5,main]
    AsyncMain.insertionSort 7
    最终结果为:7
    AsyncMain.bubbleSort 83

观察结果,插入排序先执行完毕,则优先使用插入排序的结果,冒泡排序没有被停止,也会继续执行,
但不需要他的结果了。


异常处理

异步处理中,lambda表达式中的异常抛不出来,需要使用exceptionally或者handle:

 public static void main(String[] args) throws InterruptedException {  			    	   
        CompletableFuture.runAsync(() -> {
        int a = 1/0;
    }).exceptionally(throwable -> {
        throwable.printStackTrace();
        return null;
    });
        System.out.println(Thread.currentThread());
        Thread.sleep(10000);
    }
public static void main(String[] args) throws InterruptedException { 	  	      		    
        CompletableFuture.runAsync(() -> {
        int a = 1/0;
    }).handle((aVoid, throwable) -> {
        if (throwable != null){
            throwable.printStackTrace();
        }
        System.out.println("handle");
        return null;
    });                                                              		  		 
        System.out.println(Thread.currentThread());
        Thread.sleep(10000);
    }

两者的区别是,exceptionally是当发生异常时执行,而handle则总是执行,不管是否发生异常。
其他总结:
CompletableFuture方法中带有Async都会在线程池中重开一个线程,而不带Async的使用已有的
线程。
如果调用了CompletableFuture的cancel方法,则再次调用get会抛CancellationException异常。
可以手动创建线程池替换默认的ForkjoinPool。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值