之前的多线程处理
Runnable --> Void
Callable --> Future (get() 阻塞等待结果,或者while一直等待结果)
现在的异步编程
等结果完成后,自动触发回调方法。
CompletableFunction
a) 提供要运行的任务
b)任务完成后触发
c) 处理执行流程中碰到的异常
Testing Code:
package com.coupang.flink;import org.junit.Test;import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;/*** description: TODO** @author adore.chen* @date 2022-11-18*/
public class CompletableFutureTest {@Testpublic void runAsync() {CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {System.out.println("First Runnable Task ... at " + LocalDateTime.now());try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}).thenRun(() -> System.out.println("Second Runnable Task ... at " + LocalDateTime.now()));completableFuture.join();System.out.println("Main thread finished! ");}@Testpublic void runAsyncException() {CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {System.out.println("First Runnable Task ... at " + LocalDateTime.now());throw new IllegalArgumentException("Unknown exception ...");}).thenRun(() -> System.out.println("Second Runnable Task ... at " + LocalDateTime.now())).exceptionally(e -> {System.out.println("meet some exception");e.printStackTrace(System.err);return null;});completableFuture.join();System.out.println("Main thread finished! ");}@Testpublic void supplyAsync() {CompletableFuture completableFuture = CompletableFuture.supplyAsync( () -> {System.out.println("First Callable Task ... at " + LocalDateTime.now());try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}return "First Result";}).thenAccept(r -> {System.out.println("Second Callabe Task ... at " + LocalDateTime.now());System.out.println("Received result from First Callable: " + r);});completableFuture.join();System.out.println("Main Thread Finished");}@Testpublic void supplyAsyncResult() throws ExecutionException, InterruptedException {CompletableFuture completableFuture = CompletableFuture.supplyAsync( () -> {System.out.println("First Callable Task ... at " + LocalDateTime.now());try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}return "First Result";}).thenApply(r -> {System.out.println("Second Callabe Task ... at " + LocalDateTime.now());System.out.println("Received result from First Callable: " + r);return "Second Task Result";});System.out.println("Main Thread Finished with Result: " + completableFuture.get());}public static void supplyAsyncHandle() throws ExecutionException, InterruptedException {CompletableFuture completableFuture = CompletableFuture.supplyAsync( () -> {System.out.println("First Callable Task ... at " + LocalDateTime.now());try {TimeUnit.MILLISECONDS.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}return "First Result";}).thenApply(r -> {System.out.println("Second Callabe Task ... at " + LocalDateTime.now());System.out.println("Received result from First Callable: " + r);return "Second Task Result";}).handle((data, e) -> {System.out.println("get result from first task, result: " + data);if (e != null) {e.printStackTrace();}return "handle over";});System.out.println("Main Thread Finished with Result: " + completableFuture.get());}@Testpublic void supplyAsyncHandleException() throws ExecutionException, InterruptedException {CompletableFuture completableFuture = CompletableFuture.supplyAsync( () -> {System.out.println("First Callable Task ... at " + LocalDateTime.now());throw new IllegalArgumentException("Unkown Exception !!!");}).thenApply(r -> {System.out.println("Second Callabe Task ... at " + LocalDateTime.now());System.out.println("Received result from First Callable: " + r);return "Second Task Result";}).handle((data, e) -> {System.out.println("get result from first task, result: " + data);if (e != null) {e.printStackTrace();}return "handle over";});System.out.println("Main Thread Finished with Result: " + completableFuture.get());}
}