在Java 8中, 新增加了一个包含50个方法左右的类: CompletableFuture,结合了Future的优点,提供了非常强大的Future的扩展功能,可以帮助我们简化异步编程的复杂性,提供了函数式编程的能力,可以通过回调的方式处理计算结果,并且提供了转换和组合CompletableFuture的方法。
CompletableFuture被设计在Java中进行异步编程。异步编程意味着在主线程之外创建一个独立的线程,与主线程分隔开,并在上面运行一个非阻塞的任务,然后通知主线程进展,成功或者失败。
通过这种方式,你的主线程不用为了任务的完成而阻塞/等待,你可以用主线程去并行执行其他的任务。 使用这种并行方式,极大地提升了程序的表现。
提供的能力包括:
|
例子:
runAsync
public void noReturnCompletableDate(){
CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
try {
//模拟处理某事
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("no return do something....");
});
}
supplyAsync
public CompletableFuture<Integer> returnCompletableDate(Integer time) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> cf = CompletableFuture.supplyAsync(() -> {
try {
//模拟处理某事
Thread.sleep(time * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int v =random.nextInt(100);
System.out.println("异步方法任务执行完成 Id:" + v +" "+ Thread.currentThread().getName());
return v;
});
return cf;
}
thenApplyAsync
public Integer thenApplyAsync(CompletableFuture<Integer> cf1 ) throws ExecutionException, InterruptedException {
CompletableFuture<Integer> cf2 = cf1.thenApplyAsync((result) -> {
try {
//模拟处理某事
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
result += 2;
return result;
});
System.out.println( " cf2 do something...." +cf2.get());
return cf2.get();
}
allof
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
try {
//使用sleep()模拟耗时操作
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
return 2;
});
CompletableFuture.allOf(future1, future1);
// 输出3
System.out.println(future1.join()+future2.join());
获取率先完成的任务结果——anyOf
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
throw new NullPointerException();
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
try {
// 睡眠3s模拟延时
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
});
CompletableFuture<Object> anyOf = CompletableFuture
.anyOf(future, future2)
.exceptionally(error -> {
error.printStackTrace();
return 2;
});
System.out.println(anyOf.join());
运行实例代码:
public void testGetOrderList() throws Exception {
CompletableFuture<Integer> result = returnCompletableDate(1);
System.out.println("result:" +result.get());
noReturnCompletableDate();
Integer result3 = thenApplyAsync(result);
CompletableFuture<Integer> result2 = returnCompletableDate(3);
System.out.println("result2:" +result2.get());
System.out.println("zong异步方法任务执行完成 Id:" +result);
for(int i = 1;i<10;i++){
Thread.sleep(1000);
int ss = 1;
}
}
结果截图:
![](https://img-blog.csdnimg.cn/img_convert/95bbf1c9e4f16bebeba52841eb865be4.png)
使用方式已经介绍完毕,剩下的就是分析业务并且在业务中使用了。
在线上环境中,注意一定要新建线程使用,不要使用默认的forkjoinpool。