CompletableFuture是Future和CompletionStage的一个实现。它可以通过回调的方式处理线程执行结果,也提供了转换和组合多个CompletableFuture 的方法。
CompletableFuture实例化
一共有四种实例化方法,runAsync是不带返回值的,supplyAsync是有返回值的。
第二个参数可选,表示是用哪一种方式的线程池。默认情况下使用的线程池为:ForkJoinPool.commonPool()
CompletableFuture<Void> cf1 = CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {
}
});
CompletableFuture<Void> cf2 = CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {
}
}, Executors.newFixedThreadPool(10));
CompletableFuture<String> cf3 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
return "";
}
});
CompletableFuture<String> cf4 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
return "";
}
} , Executors.newFixedThreadPool(10));
获取线程执行结果,使用的方法为get()
String res = future.get();
返回类型与定义对象使用的泛型相同。
CompletableFuture中的方法
thenApply
该方法使用的参数为Function<A,B>第一个泛型为输入参数的类型,第二个参数为输出参数的类型。该方法返回类型为CompletableFuture。
private static void thenApply() throws Exception {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
long result = new Random().nextInt(100);
System.out.println("result1=" + result);
return result;
}).thenApply((t) -> {
long result = t * 5;
System.out.println("result2=" + result);
return result;
}).thenApply(new Function<Long, String>() {
@Override
public String apply(Long t) {
return null;
}
});
String result = future.get();
System.out.println(result);
}
thenAccept
该方法与上面的方法最大的区别在于参数为Consumer<A> 只接受上级方法返回参数,不对处理结果进行返回,是消费式的。
public static void thenAccept() throws Exception {
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
return new Random().nextInt(10);
}).thenAccept(integer -> {
System.out.println(integer);
});
future.get();
}
thenRun
该方法在上一步方法执行之后执行。没有输入参数和输出参数。
public static void thenRun() throws Exception {
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
return new Random().nextInt(10);
}).thenRun(() -> {
System.out.println("thenRun ...");
});
future.get();
}
handle
用于方法执行后执行,可以接受上级处理结果和上级方法异常。
public static void handle() throws Exception {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int i = 10 / 0;
return new Random().nextInt(10);
}).handle((param, throwable) -> {
int result = -1;
if (throwable == null) {
result = param * 2;
} else {
System.out.println(throwable.getMessage());
}
return result;
});
System.out.println(future.get());
}
exceptionally与whenComplete
whenComplete对执行结果进行处理,不影响get方法获取最初的返回值。当发生异常后exceptionally方法会执行。对异常进行处理。
public static void whenCompleteWithSupply() throws Exception {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
if (new Random().nextInt() % 2 >= 0) {
System.out.println("异常。。。。");
int i = 12 / 0;
}
System.out.println("ok。。。。");
return "true result";
}).exceptionally(new Function<Throwable, String>() {
@Override
public String apply(Throwable t) {
System.out.println("false result");
return "false result";
}
}).whenComplete((t, action) -> {
System.out.println("上一步执行结果" + t);
});
System.out.println("获取结果"+future.get());
}