- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
- public class CompletableFutureTest {
-
- public static ExecutorService executorService = Executors.newFixedThreadPool(10);
-
- public static void main(String[] args) {
- System.out.println("Main method is running");
-
- /**
- * public static CompletableFuture
runAsync(Runnable runnable) { - * return asyncRunStage(asyncPool, runnable);
- * }
- * public static CompletableFuture
runAsync(Runnable runnable, - * Executor executor) {
- * return asyncRunStage(screenExecutor(executor), runnable);
- * }
- */
- // CompletableFuture
future = CompletableFuture.runAsync(() -> { - // System.out.println("Current thread is " + Thread.currentThread().getId());
- // int i = 8 / 2;
- // System.out.println("Running result is " + i);
- // }, executorService);
-
- /**
- *public static CompletableFuture supplyAsync(Supplier supplier) {
- * return asyncSupplyStage(asyncPool, supplier);
- *}
- *public static CompletableFuture supplyAsync(Supplier supplier,
- * Executor executor) {
- * return asyncSupplyStage(screenExecutor(executor), supplier);
- *}
- */
- CompletableFuture
future = CompletableFuture.supplyAsync(()->{ - System.out.println("Current thread is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result is " + i);
- return i;
- },executorService);
- try {
- Integer i = future.get();
- System.out.println("i is "+i);
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
-
-
- System.out.println("Main method is end");
- }
- }
2.提供回调方法
- CompletableFuture
future = CompletableFuture.supplyAsync(()->{ - System.out.println("Current thread is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result is " + i);
- return i;
- },executorService).whenComplete((result,exception)->{
- //虽然能得到异常信息,但不能修改返回结果
- System.out.println("The result is "+result+" ,the exception is "+exception);
- }).exceptionally(throwable -> {
- //可以感知异常当出现错误时,默认返回-1
- return -1;
- });
- try {
- Integer i = future.get();
- System.out.println("i is "+i);
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
3.handle方法
- //方法执行完后的处理
- CompletableFuture
future = CompletableFuture.supplyAsync(()->{ - System.out.println("Current thread is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result is " + i);
- return i;
- },executorService).handle((result,throwable)->{
- if(result!=null){
- return result;
- }
- if(throwable!=null){
- return 0;
- }
- return -1;
- });
- try {
- Integer i = future.get();
- System.out.println("i is "+i);
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
得到结果:
Running result is 4
i is 4
将8/2改成8/0,得到结果
i is 0
4.线程串行化
- //不能获取到上一步的执行结果
- CompletableFuture
future1 = CompletableFuture.supplyAsync(()->{ - System.out.println("Current thread is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result is " + i);
- return i;
- /**
- * public CompletableFuture
thenRun(Runnable action) { - * return uniRunStage(null, action);
- * }
- *
- * public CompletableFuture
thenRunAsync(Runnable action) { - * return uniRunStage(asyncPool, action);
- * }
- *
- * public CompletableFuture
thenRunAsync(Runnable action, - * Executor executor) {
- * return uniRunStage(screenExecutor(executor), action);
- * }
- */
- },executorService).thenRunAsync(()->{
- System.out.println("Task 2 starts");
- },executorService);
- //能接受上一步的结果,但无返回值
- CompletableFuture
future1 = CompletableFuture.supplyAsync(()->{ - System.out.println("Current thread is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result is " + i);
- return i;
- /**
- * public CompletableFuture
thenAccept(Consumer super T> action) { - * return uniAcceptStage(null, action);
- * }
- *
- * public CompletableFuture
thenAcceptAsync(Consumer super T> action) { - * return uniAcceptStage(asyncPool, action);
- * }
- *
- * public CompletableFuture
thenAcceptAsync(Consumer super T> action, - * Executor executor) {
- * return uniAcceptStage(screenExecutor(executor), action);
- * }
- */
- },executorService).thenAcceptAsync((result)->{
- System.out.println("Task 2 starts,the result of the previous step is "+result);
- },executorService);
- //能接受到上一步的结果,并且本步骤也有返回值
- CompletableFuture
future1 = CompletableFuture.supplyAsync(()->{ - System.out.println("Current thread is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result is " + i);
- return i;
- /**
- * public CompletableFuture thenApply(
- * Function super T,? extends U> fn) {
- * return uniApplyStage(null, fn);
- * }
- *
- * public CompletableFuture thenApplyAsync(
- * Function super T,? extends U> fn) {
- * return uniApplyStage(asyncPool, fn);
- * }
- *
- * public CompletableFuture thenApplyAsync(
- * Function super T,? extends U> fn, Executor executor) {
- * return uniApplyStage(screenExecutor(executor), fn);
- * }
- */
- },executorService).thenApplyAsync((result)->{
- System.out.println("Task 2 starts,the result of the previous step is "+result);
- return "This step has result too,"+result;
- },executorService);
- try {
- System.out.println(future1.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
5.两任务组合 - 都要完成
- CompletableFuture
future1 = CompletableFuture.supplyAsync(() -> { - System.out.println("Thread01 is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result01 is " + i);
- return i;
- }, executorService);
- CompletableFuture
future2 = CompletableFuture.supplyAsync(() -> { - System.out.println("Thread02 is " + Thread.currentThread().getId());
- System.out.println("Running result02 is ");
- return "hello";
- }, executorService);
- //得不到上两个任务的结果,无返回值
- // future1.runAfterBothAsync(future2,()->{
- // System.out.println("task 3 is running");
- // },executorService);
- //得到上两个任务的结果,无返回值
- // future1.thenAcceptBothAsync(future2,(result1,result2)->{
- // System.out.println("Get previous results "+result1+","+result2);
- // },executorService);
- //得到上两个任务的结果,有返回值
- CompletableFuture
future = future1.thenCombineAsync(future2, (result1, result2) -> { - return result1 + result2;
- }, executorService);
- try {
- System.out.println("The result obtained by combining the two results "+future.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
6.两任务组合 - 完成其中一个即可
- CompletableFuture
future1 = CompletableFuture.supplyAsync(() -> { - System.out.println("Thread01 is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result01 is " + i);
- return i;
- }, executorService);
- CompletableFuture
future2 = CompletableFuture.supplyAsync(() -> { - System.out.println("Thread02 is " + Thread.currentThread().getId());
- int i = 10 / 2;
- System.out.println("Running result02 is ");
- return i;
- }, executorService);
- // future1.runAfterEitherAsync(future2,()->{
- // System.out.println("task 3 is running");
- // },executorService);
- // future1.acceptEitherAsync(future2,(result)->{
- // System.out.println("Get previous results "+result);
- // },executorService);
- CompletableFuture
future = future1.applyToEitherAsync(future2, (result) -> { - return result;
- }, executorService);
- try {
- System.out.println("The result obtained by combining the result "+future.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }
7.多任务组合
- CompletableFuture
future1 = CompletableFuture.supplyAsync(() -> { - System.out.println("Thread01 is " + Thread.currentThread().getId());
- int i = 8 / 2;
- System.out.println("Running result01 is " + i);
- return i;
- }, executorService);
- CompletableFuture
future2 = CompletableFuture.supplyAsync(() -> { - System.out.println("Thread02 is " + Thread.currentThread().getId());
- int i = 10 / 2;
- System.out.println("Running result02 is " + i);
- return i;
- }, executorService);
- CompletableFuture
future3 = CompletableFuture.supplyAsync(() -> { - System.out.println("Thread03 is " + Thread.currentThread().getId());
- int i = 12 / 2;
- System.out.println("Running result03 is " + i);
- return i;
- }, executorService);
- CompletableFuture
- //CompletableFuture
allOf = CompletableFuture.allOf(future1, future2, future3); - try {
- anyOf.get();//只要有一个执行成功即可
- System.out.println(anyOf.get());
- //allOf.get();//等待所有结果完成
- //System.out.println(future1.get()+","+future2.get()+","+future3.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
- }