JUC_CompletableFuture異步編排

創建異步對象

公有線程池

public static ExecutorService executor=new ThreadPoolExecutor(5, 10, 100, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20));

有返回值supplyAsync

CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
	return 1 + 1;
}, executor);
System.out.println(future.get());

無返回值runAsync

CompletableFuture.runAsync(()->{
    int i=1+1;
}, executor);

任務完成時回調方法

任務完成後感知結果和異常whenCompleteAsync、exceptionally

CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
    return 1 + 1;
    // 感知到異常,無法修改結果
}, executor).whenCompleteAsync((result,exception)->{
    System.out.println(result+":"+exception);
    // 感知到異常,同時返回另外值(非結果相關值)
},executor).exceptionally(exception->{
   return 1+1;
});

任務完成後處理結果和異常handle

CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> {
    return 1 + 1;
}, executor).handle((result,exception)->{
    if(result!=null){
        return  result;
    }
    return -1;
});

線程串行化

不接收前一任務的結果,自身無返回值thenRunAsync

CompletableFuture<Void> future4 = CompletableFuture.supplyAsync(() -> {
    return 1 + 1;
}, executor).thenRunAsync(()->{
    System.out.println("silence");
}, executor);

接收前一任務的結果,自身無返回值thenAcceptAsync

CompletableFuture<Void> future5 = CompletableFuture.supplyAsync(() -> {
    return 1 + 1;
}, executor).thenAcceptAsync(System.out::println, executor);

接收前一任務的結果,自身有返回值thenApplyAsync

CompletableFuture<Object> future6 = CompletableFuture.supplyAsync(() -> {
    return 1 + 1;
}, executor).thenApplyAsync(result->{
    return result*2;
},executor);
System.out.println(future6.get());

兩任務完成後,執行第三任務

CompletableFuture<Integer> future7 = CompletableFuture.supplyAsync(() -> {
    System.out.println(1);
    return 1 + 1;
}, executor);

CompletableFuture<Integer> future8 = CompletableFuture.supplyAsync(() -> {
    System.out.println(2);
    return 2 + 2;
}, executor);

//runAfterBoth不能獲取前兩者結果
future7.runAfterBothAsync(future8, ()->{
    System.out.println(3);
}, executor);
//thenAcceptBoth獲取兩者返回值,無返回值
future7.thenAcceptBothAsync(future8, (r1,r2)->{
    System.out.println(r1+r2);
},executor);
//thenCombine獲取兩者返回值,有返回值
CompletableFuture<Integer> future9 = future7.thenCombineAsync(future8, Integer::sum, executor);
System.out.println(future9.get());

兩任務之一完成後,執行第三任務

//runAfterEither不感知結果,無返回值
future7.runAfterEitherAsync(future8, ()->{
    System.out.println("one completed");
}, executor);
//acceptEither 感知第一個完成的結果,無返回值
future7.acceptEitherAsync(future8, System.out::println, executor);
//applyToEither感知第一個完成的結果,有返回值
future7.applyToEitherAsync(future8, result->{
    return result*2;
}, executor);

多任務組合

CompletableFuture<Integer> future11 = CompletableFuture.supplyAsync(() -> {
    System.out.println(1);
    return 1 + 1;
}, executor);

CompletableFuture<Integer> future12 = CompletableFuture.supplyAsync(() -> {
    System.out.println(2);
    return 2 + 2;
}, executor);

CompletableFuture<Integer> future13 = CompletableFuture.supplyAsync(() -> {
    System.out.println(3);
    return 3 + 3;
}, executor);
// 等待所有任務都完成
CompletableFuture<Void> allOf = CompletableFuture.allOf(future11, future12, future13);
//無返回值
allOf.get();
System.out.println(future11.get()+":"+future12.get()+":"+future13.get());

//有一個任務完成,返回值爲完成任務的返回值
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future11, future12, future13);
Object o = anyOf.get();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章