- 指定线程池执行
thenRunAsync
后续任务:
thenRunAsync
方法有一个重载形式可以接受Executor
参数,通过这个参数可以指定线程池来执行后续任务。
- 代码示例:
import java.util.concurrent.*;
public class CompletableFutureExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
CompletableFuture.supplyAsync(() -> {
System.out.println("Initial task executed by: " + Thread.currentThread().getName());
return "Result";
}).thenRunAsync(() -> {
System.out.println("Task in thenRunAsync executed by: " + Thread.currentThread().getName());
}, executor).whenComplete((result, throwable) -> {
executor.shutdown();
});
}
}
- 在上述代码中,
supplyAsync
方法启动一个异步任务,thenRunAsync
方法的第二个参数executor
指定了线程池来执行thenRunAsync
中的任务。
- 前面任务抛出异常时
thenRunAsync
任务是否执行:
- 如果前面的任务抛出异常,
thenRunAsync
方法中的任务不会执行。
- 代码示例:
import java.util.concurrent.*;
public class CompletableFutureExceptionExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
CompletableFuture.supplyAsync(() -> {
System.out.println("Initial task executed by: " + Thread.currentThread().getName());
throw new RuntimeException("Simulated exception");
}).thenRunAsync(() -> {
System.out.println("Task in thenRunAsync executed by: " + Thread.currentThread().getName());
}, executor).whenComplete((result, throwable) -> {
if (throwable != null) {
System.out.println("Caught exception: " + throwable.getMessage());
}
executor.shutdown();
});
}
}
- 在上述代码中,
supplyAsync
方法中抛出了一个运行时异常,thenRunAsync
中的任务不会执行,通过whenComplete
方法捕获到了异常。
- 异常处理机制:
CompletableFuture
提供了多种异常处理方法,如whenComplete
、exceptionally
等。
whenComplete
方法:它接收两个参数,第一个是正常完成时的结果(如果有),第二个是异常(如果有)。通过检查第二个参数是否为null
来判断是否发生异常。
- 代码示例:
import java.util.concurrent.*;
public class CompletableFutureExceptionHandling {
public static void main(String[] args) {
CompletableFuture.supplyAsync(() -> {
if (Math.random() < 0.5) {
throw new RuntimeException("Random exception");
}
return "Success";
}).whenComplete((result, throwable) -> {
if (throwable != null) {
System.out.println("Caught exception in whenComplete: " + throwable.getMessage());
} else {
System.out.println("Result: " + result);
}
});
}
}
exceptionally
方法:它接收一个Function
,该Function
的参数为异常,返回值是在异常发生时要返回的替代结果。
- 代码示例:
import java.util.concurrent.*;
public class CompletableFutureExceptionallyExample {
public static void main(String[] args) {
CompletableFuture.supplyAsync(() -> {
if (Math.random() < 0.5) {
throw new RuntimeException("Random exception");
}
return "Success";
}).exceptionally(throwable -> {
System.out.println("Caught exception in exceptionally: " + throwable.getMessage());
return "Default value";
}).thenAccept(System.out::println);
}
}
- 在上述代码中,
exceptionally
方法捕获到异常并返回了一个默认值。