CompletableFuture api詳解

花了兩天時間學習CompletableFuture api,該類是JUC原子包中的類,通過單元測試代碼把所有public api方法跑了一遍,大致瞭解了底層實現,初學乍練,有很多一知半解的地方,待後續有了深入理解再來補充

package test.java.util.concurrent;


import java.util.concurrent.*;
import java.util.function.*;

import org.junit.Test;

import static java.lang.Thread.*;

/**
 * CompletableFuture的測試類
 *
 * @author zqw
 * @date 2020-06-29 21:33:34
 */
public class CompletableFutureTest {
        /**
        *無參構造函數
        * @Param
        * @author zqw
        */
        @Test
        public void testConstruct0()throws Exception{
        CompletableFuture testObj=new CompletableFuture();
                System.out.println(testObj.toString());
        }
        /**
         * supplier異步處理
         * @Param
         * @author zqw
         */
        @Test
        public void testSupplyAsync1()throws Exception{
                Supplier supplier= ()->33*33333333+321321;
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                System.out.println(future.get());
        }
        /**
         *supplier異步處理,指定自己的線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testSupplyAsync()throws Exception{
                Supplier supplier= ()->33*3+1;
                Executor executor= ForkJoinPool.commonPool();
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier,executor);
                System.out.println(future.get());
        }
        /**
         *異步運行runnable實現,返回空的future,內部
         * 使用線程池運行
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAsync1()throws Exception{
                Runnable runnable=() -> System.out.println(23123);
                CompletableFuture<Void> completableFuture=CompletableFuture.runAsync(runnable);
                System.out.println(completableFuture.get());
        }
        /**
         *異步運行runnable實現,返回空的future,使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAsync()throws Exception{
                Executor executor=ForkJoinPool.commonPool();
                Runnable runnable=() -> System.out.println(23123);
                CompletableFuture<Void> completableFuture=CompletableFuture.runAsync(runnable,executor);
                System.out.println(completableFuture.get());
        }
        /**
         * 返回使用給定value值的CompletableFuture
         * @Param
         * @author zqw
         */
        @Test
        public void testCompletedFuture()throws Exception{
                CompletableFuture<Long> future=CompletableFuture.completedFuture(3243L);
                System.out.println(future.get());
        }
        /**
         * 判斷是否完成,完成即返回結果,result有值
         * @Param
         * @author zqw
         */
        @Test
        public void testIsDone()throws Exception{
                CompletableFuture<Long> future=CompletableFuture.completedFuture(3243L);
                System.out.println(future.isDone());
                System.out.println(future.get());
                System.out.println(future.isDone());
        }
        /**
         *完成則返回值,未完成則阻塞等待
         * @Param
         * @author zqw
         */
        @Test
        public void testGet1()throws Exception{
                CompletableFuture<Long> future=CompletableFuture.completedFuture(3243L);
                System.out.println(future.get());
        }
        /**
         * 完成則返回值,未完成則阻塞等待參數時間
         * @Param
         * @author zqw
         */
        @Test
        public void testGet()throws Exception{
                CompletableFuture<Long> future=CompletableFuture.completedFuture(3243L);
                System.out.println(future.get(1000, TimeUnit.SECONDS));
        }
        /**
         * 溫和處理方式,獲取返回值,沒有處理完成則阻塞,不會阻斷
         * @Param
         * @author zqw
         */
        @Test
        public void testJoin()throws Exception{
                CompletableFuture<Long> future=CompletableFuture.completedFuture(3243L);
                System.out.println(future.join());
        }
        /**
         * 立即獲取結果,沒有處理完則將指定參數作爲結果返回
         * @Param
         * @author zqw
         */
        @Test
        public void testGetNow()throws Exception{
                Supplier supplier=()-> {
                        try {
                                 sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                System.out.println(future.getNow(12));
        }
        /**
         * 將給定參數作爲結果完成異步處理
         * @Param
         * @author zqw
         */
        @Test
        public void testComplete()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                System.out.println(future.complete(12));
                System.out.println(future.get());
        }
        /**
         * 將指定異常作爲處理結果返回
         * @Param
         * @author zqw
         */
        @Test
        public void testCompleteExceptionally()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                System.out.println(future.completeExceptionally(new Throwable("321321")));
                System.out.println(future.get());
        }
        /**
         *轉換泛型中的類型,如下例子中將Integer轉換成Object
         * @Param
         * @author zqw
         */
        @Test
        public void testThenApply()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Function function=a  -> 333*3;
                CompletableFuture<Object> future1=future.thenApply(function);
                System.out.println(future.get());
                System.out.println(future1.get());

        }
        /**
         *轉換泛型中的類型,如下例子中將Integer轉換成Object
         * 使用內部線程池異步處理
         * @Param
         * @author zqw
         */
        @Test
        public void testThenApplyAsync1()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Function function=a  -> 333*3;
                CompletableFuture<Object> future1=future.thenApplyAsync(function);
                System.out.println(future.get());
                System.out.println(future1.get());
        }
        /**
         *轉換泛型中的類型,如下例子中將Integer轉換成Object
         * 使用指定線程池異步處理
         * @Param
         * @author zqw
         */
        @Test
        public void testThenApplyAsync()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Function function=a  -> 333*3;
                CompletableFuture<Object> future1=future.thenApplyAsync(function,ForkJoinPool.commonPool());
                System.out.println(future.get());
                System.out.println(future1.get());
        }
        /**
         *執行consumer 並得到新的空的future,舊的不受影響
         * @Param
         * @author zqw
         */
        @Test
        public void testThenAccept()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Consumer<Integer> consumer= a  -> System.out.println(333);
                CompletableFuture<Void> future1=future.thenAccept(consumer);
                System.out.println(future.get());
                System.out.println(future1.get());
        }
        /**
         *異步執行consumer 並得到新的空的future,舊的不受影響,使用內部線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenAcceptAsync1()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Consumer<Integer> consumer= a  -> System.out.println(333);
                CompletableFuture<Void> future1=future.thenAcceptAsync(consumer);
                System.out.println(future.get());
                System.out.println(future1.get());
        }
        /**
         *異步執行consumer 並得到新的空的future,舊的不受影響,使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenAcceptAsync()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Consumer<Integer> consumer= a  -> System.out.println(333);
                CompletableFuture<Void> future1=future.thenAcceptAsync(consumer,ForkJoinPool.commonPool());
                System.out.println(future.get());
                System.out.println(future1.get());
        }
        /**
         * 運行runnable,沒有返回值
         * @Param
         * @author zqw
         */
        @Test
        public void testThenRun()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Runnable runnable=() -> System.out.println(33333);
                CompletableFuture<Void> future1=future.thenRun(runnable);
                System.out.println(future1.get());
        }
        /**
         *異步運行runnable,沒有返回值
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenRunAsync1()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Runnable runnable=() -> System.out.println(33333);
                CompletableFuture<Void> future1=future.thenRunAsync(runnable);
                System.out.println(future1.get());
        }
        /**
         *異步運行runnable,沒有返回值
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenRunAsync()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return 2333;
                };
                CompletableFuture<Integer> future=CompletableFuture.supplyAsync(supplier);
                Runnable runnable=() -> System.out.println(33333);
                CompletableFuture<Void> future1=future.thenRunAsync(runnable,ForkJoinPool.commonPool());
                System.out.println(future1.get());
        }
        /**
         * 將兩個線程的執行結果合併,然後返回
         * @Param
         * @author zqw
         */
        @Test
        public void testThenCombine()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                Supplier supplier1=() ->{
                        try {
                                TimeUnit.SECONDS.sleep(3);
                        }catch (InterruptedException e){
                                e.printStackTrace();
                        }
                        return "qwqq";
                };
                BiFunction<String,String,String> biFunction=(String s,String s2) ->s+s2;
                CompletableFuture<String> future1=future.thenCombine(CompletableFuture.supplyAsync(supplier1),biFunction );
                System.out.println(future1.get());
        }
        /**
         *將兩個線程的執行結果合併,然後返回
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenCombineAsync1()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                Supplier supplier1=() ->{
                        try {
                                TimeUnit.SECONDS.sleep(3);
                        }catch (InterruptedException e){
                                e.printStackTrace();
                        }
                        return "qwqq";
                };
                BiFunction<String,String,String> biFunction=(String s,String s2) ->s+s2;
                CompletableFuture<String> future1=future.thenCombineAsync(CompletableFuture.supplyAsync(supplier1),biFunction );
                System.out.println(future1.get());
        }
        /**
         *將兩個線程的執行結果合併,然後返回
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenCombineAsync()throws Exception{
                Supplier supplier=()-> {
                        try {
                                sleep(2000);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                Supplier supplier1=() ->{
                        try {
                                TimeUnit.SECONDS.sleep(3);
                        }catch (InterruptedException e){
                                e.printStackTrace();
                        }
                        return "qwqq";
                };
                BiFunction<String,String,String> biFunction=(String s,String s2) ->s+s2;
                CompletableFuture<String> future1=future.thenCombineAsync(CompletableFuture.supplyAsync(supplier1),biFunction ,ForkJoinPool.commonPool());
                System.out.println(future1.get());
        }
        /**
         * 合併兩次線程執行結果,並用consumer執行兩次結果
         * @Param
         * @author zqw
         */
        @Test
        public void testThenAcceptBoth()throws Exception{
                Supplier supplier=()-> "31321";
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                BiConsumer<String,String> biConsumer=(s,s2) -> System.out.println(s+s2);
                CompletableFuture<Void> future1=future.thenAcceptBoth(CompletableFuture.supplyAsync(supplier1),biConsumer);
                System.out.println(future1.get());
        }
        /**
         *異步合併兩次線程執行結果,並用consumer執行兩次結果
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenAcceptBothAsync1()throws Exception{
                Supplier supplier=()-> "31321";
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                BiConsumer<String,String> biConsumer=(s,s2) -> System.out.println(s+s2);
                CompletableFuture<Void> future1=future.thenAcceptBothAsync(CompletableFuture.supplyAsync(supplier1),biConsumer);
                System.out.println(future1.get());
        }
        /**
         *異步合併兩次線程執行結果,並用consumer執行兩次結果
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenAcceptBothAsync()throws Exception{
                Supplier supplier=()-> "31321";
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                BiConsumer<String,String> biConsumer=(s,s2) -> System.out.println(s+s2);
                CompletableFuture<Void> future1=future.thenAcceptBothAsync(CompletableFuture.supplyAsync(supplier1),biConsumer,ForkJoinPool.commonPool());
                System.out.println(future1.get());
        }
        /**
         * future和future2 執行完之後執行runnable2,future和future2順序不定
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAfterBoth()throws Exception{
                Runnable runnable=()-> System.out.println(1);
                Runnable runnable1=()-> System.out.println(2);
                CompletableFuture<Void> future=CompletableFuture.runAsync(runnable);
                CompletableFuture<Void> future2=CompletableFuture.runAsync(runnable1);
                Runnable runnable2=()-> System.out.println(3);
                CompletableFuture<Void> future1=future2.runAfterBoth(future,runnable2);
        }
        /**
         *future和future2 執行完之後執行runnable2,future和future2順序不定
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAfterBothAsync1()throws Exception{
                Runnable runnable=()-> System.out.println(1);
                Runnable runnable1=()-> System.out.println(2);
                CompletableFuture<Void> future=CompletableFuture.runAsync(runnable);
                CompletableFuture<Void> future2=CompletableFuture.runAsync(runnable1);
                Runnable runnable2=()-> System.out.println(3);
                CompletableFuture<Void> future1=future2.runAfterBothAsync(future,runnable2);
        }
        /**
         *future和future2 執行完之後執行runnable2,future和future2順序不定
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAfterBothAsync()throws Exception{
                Runnable runnable=()-> System.out.println(1);
                Runnable runnable1=()-> System.out.println(2);
                CompletableFuture<Void> future=CompletableFuture.runAsync(runnable);
                CompletableFuture<Void> future2=CompletableFuture.runAsync(runnable1);
                Runnable runnable2=()-> System.out.println(3);
                CompletableFuture<Void> future1=future2.runAfterBothAsync(future,runnable2,ForkJoinPool.commonPool());
        }
        /**
         * 將future和future1最快返回的結果作爲接下來線程執行的輸入
         * @Param
         * @author zqw
         */
        @Test
        public void testApplyToEither()throws Exception{
                Supplier supplier=()-> {
                        try {
                                TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                CompletableFuture<String> future1= CompletableFuture.supplyAsync(supplier1);
                Function<String,String> function=(s) -> s+"qw";
                CompletableFuture<String> future2=future.applyToEither(future1,function);
                System.out.println(future2.get());
        }
        /**
         *將future和future1最快返回的結果作爲接下來線程執行的輸入
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testApplyToEitherAsync1()throws Exception{
                Supplier supplier=()-> {
                        try {
                                TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                CompletableFuture<String> future1= CompletableFuture.supplyAsync(supplier1);
                Function<String,String> function=(s) -> s+"qw";
                CompletableFuture<String> future2=future.applyToEitherAsync(future1,function);
                System.out.println(future2.get());
        }
        /**
         *將future和future1最快返回的結果作爲接下來線程執行的輸入
         *使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testApplyToEitherAsync()throws Exception{
                Supplier supplier=()-> {
                        try {
                                TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                CompletableFuture<String> future1= CompletableFuture.supplyAsync(supplier1);
                Function<String,String> function=(s) -> s+"qw";
                CompletableFuture<String> future2=future.applyToEitherAsync(future1,function,ForkJoinPool.commonPool());
                System.out.println(future2.get());
        }
        /**
         *將future和future1最快返回的結果作爲接下來線程執行的輸入
         * @Param
         * @author zqw
         */
        @Test
        public void testAcceptEither()throws Exception{
                Supplier supplier=()-> {
                        try {
                                TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                CompletableFuture<String> future1= CompletableFuture.supplyAsync(supplier1);
                Consumer<String> consumer=(s) -> System.out.println(s+"qw");
                CompletableFuture<Void> future2=future.acceptEither(future1,consumer);
                System.out.println(future2.get());
        }
        /**
         *將future和future1最快異步執行返回的結果作爲接下來線程執行的輸入
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testAcceptEitherAsync1()throws Exception{
                Supplier supplier=()-> {
                        try {
                                TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                CompletableFuture<String> future1= CompletableFuture.supplyAsync(supplier1);
                Consumer<String> consumer=(s) -> System.out.println(s+"qw");
                CompletableFuture<Void> future2=future.acceptEitherAsync(future1,consumer);
                System.out.println(future2.get());
        }
        /**
         *將future和future1最快異步執行返回的結果作爲接下來線程執行的輸入
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testAcceptEitherAsync()throws Exception{
                Supplier supplier=()-> {
                        try {
                                TimeUnit.SECONDS.sleep(2);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        return "31321";
                };
                Supplier supplier1=() -> "qwqq";
                CompletableFuture<String> future=CompletableFuture.supplyAsync(supplier);
                CompletableFuture<String> future1= CompletableFuture.supplyAsync(supplier1);
                Consumer<String> consumer=(s) -> System.out.println(s+"qw");
                CompletableFuture<Void> future2=future.acceptEitherAsync(future1,consumer,ForkJoinPool.commonPool());
                System.out.println(future2.get());
        }
        /**
         *runnable1和runnable2有一個執行完,執行runnable3
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAfterEither()throws Exception{
                for (int i = 0; i < 500; i++) {
                        Runnable runnable1=()-> System.out.println("run1");
                        Runnable runnable2=()-> System.out.println("run2");
                        Runnable runnable3=()-> System.out.println("run3");
                        CompletableFuture<Void> future=CompletableFuture.runAsync(runnable1);
                        CompletableFuture<Void> future1= CompletableFuture.runAsync(runnable2);
                        future.runAfterEither(future1,runnable3);
                        Thread.sleep(500);
                        System.out.println("==============================");
                }

        }
        /**異步runnable1和runnable2有一個執行完,執行runnable3
         * 使用默認線程池
         *
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAfterEitherAsync1()throws Exception{
                for (int i = 0; i < 500; i++) {
                        Runnable runnable1=()-> System.out.println("run1");
                        Runnable runnable2=()-> System.out.println("run2");
                        Runnable runnable3=()-> System.out.println("run3");
                        CompletableFuture<Void> future=CompletableFuture.runAsync(runnable1);
                        CompletableFuture<Void> future1= CompletableFuture.runAsync(runnable2);
                        future.runAfterEitherAsync(future1,runnable3);
                        Thread.sleep(500);
                        System.out.println("==============================");
                }
        }
        /**
         *runnable1和runnable2有一個執行完,執行runnable3
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testRunAfterEitherAsync()throws Exception{
                for (int i = 0; i < 500; i++) {
                        Runnable runnable1=()-> System.out.println("run1");
                        Runnable runnable2=()-> System.out.println("run2");
                        Runnable runnable3=()-> System.out.println("run3");
                        CompletableFuture<Void> future=CompletableFuture.runAsync(runnable1);
                        CompletableFuture<Void> future1= CompletableFuture.runAsync(runnable2);
                        future.runAfterEitherAsync(future1,runnable3,ForkJoinPool.commonPool());
                        Thread.sleep(500);
                        System.out.println("==============================");
                }
        }
        /**
         * 將第一個future執行結果和第二個在第二個future中計算
         * @Param
         * @author zqw
         */
        @Test
        public void testThenCompose()throws Exception{
               CompletableFuture<String>  future=CompletableFuture.supplyAsync(()-> "pp1").thenCompose(s-> CompletableFuture.supplyAsync(()->s+"pp2"));
                System.out.println(future.get());
        }
        /**
         *將第一個future執行結果和第二個在第二個future中計算(異步執行)
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenComposeAsync1()throws Exception{
                CompletableFuture<String>  future=CompletableFuture.supplyAsync(()-> "pp1").thenComposeAsync(s-> CompletableFuture.supplyAsync(()->s+"pp2"));
                System.out.println(future.get());
        }
        /**
         *將第一個future執行結果和第二個在第二個future中計算(異步執行)
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testThenComposeAsync()throws Exception{
                CompletableFuture<String>  future=CompletableFuture.supplyAsync(()-> "pp1").thenComposeAsync(s-> CompletableFuture.supplyAsync(()->s+"pp2"),ForkJoinPool.commonPool());
                System.out.println(future.get());
        }
        /**
         * 使用線程池執行future,然後使用主線程執行future2,
         * @Param
         * @author zqw
         */
        @Test
        public void testWhenComplete()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                return "1";
                        }
                );
                //主線程
                System.out.println(Thread.currentThread().getName());
                CompletableFuture<String>  future2=future.whenComplete((s,throwable)-> {
                        System.out.println(s);
                        System.out.println(throwable);
                        System.out.println("2  "+Thread.currentThread().getName());
                });
                future2.join();
                System.out.println(future2.get());
        }
        /**
         *使用線程池執行future,然後使用主線程執行future2,使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testWhenCompleteAsync1()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                return "1";
                        }
                );
                //主線程
                System.out.println(Thread.currentThread().getName());
                CompletableFuture<String>  future2=future.whenCompleteAsync((s,throwable)-> {
                        System.out.println(s);
                        System.out.println(throwable);
                        System.out.println("2  "+Thread.currentThread().getName());
                });
                future2.join();
                System.out.println(future2.get());
        }
        /**
         *使用線程池執行future,然後使用主線程執行future2,使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testWhenCompleteAsync()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                return "1";
                        }
                );
                //主線程
                System.out.println(Thread.currentThread().getName());
                CompletableFuture<String>  future2=future.whenCompleteAsync((s,throwable)-> {
                        System.out.println(s);
                        System.out.println(throwable);
                        System.out.println("2  "+Thread.currentThread().getName());
                },ForkJoinPool.commonPool());
                future2.join();
                System.out.println(future2.get());
        }
        /**
         * 使用ForkJoinPool執行future,將future執行結果作爲下一個線程輸入即主線程執行future2,
         * @Param
         * @author zqw
         */
        @Test
        public void testHandle()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                return "1";
                        }
                );
                CompletableFuture<String>  future2=future.handle((s,s1)-> {
                        System.out.println(s);
                        System.out.println(s1);
                        System.out.println("2 "+Thread.currentThread().getName());
                        return "2";
                });
                future2.join();
                System.out.println(future2.get());
        }
        /**
         *使用ForkJoinPool執行future,將future執行結果作爲下一個線程輸入即主線程執行future2(異步執行)
         * 使用默認線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testHandleAsync1()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                return "1";
                        }
                );
                CompletableFuture<String>  future2=future.handleAsync((s,s1)-> {
                        System.out.println(s);
                        System.out.println(s1);
                        System.out.println("2 "+Thread.currentThread().getName());
                        return "2";
                });
                future2.join();
                System.out.println(future2.get());
        }
        /**
         *使用ForkJoinPool執行future,將future執行結果作爲下一個線程輸入即主線程執行future2(異步執行)
         * 使用指定線程池
         * @Param
         * @author zqw
         */
        @Test
        public void testHandleAsync()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                return "1";
                        }
                );
                CompletableFuture<String>  future2=future.handleAsync((s,s1)-> {
                        System.out.println(s);
                        System.out.println(s1);
                        System.out.println("2 "+Thread.currentThread().getName());
                        return "2";
                },ForkJoinPool.commonPool());
                future2.join();
                System.out.println(future2.get());
        }
        /**
         * 返回當前future的引用
         * @Param
         * @author zqw
         */
        @Test
        public void testToCompletableFuture()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                return "1";
                        }
                );
                CompletableFuture<String>  future2=future.toCompletableFuture();
                System.out.println(future.get());
                System.out.println(future2.get());
        }
        /**
         *future執行過程中遇到異常轉向exceptionally代碼塊
         * @Param
         * @author zqw
         */
        @Test
        public void testExceptionally()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                                System.out.println("1  "+Thread.currentThread().getName());
                                if (true){
                                        throw new NullPointerException("333");
                                }
                                return "1";
                        }
                );
                CompletableFuture<String>  future2=future.exceptionally((s )-> s+"222");
                System.out.println(future2.get());
        }
        /**
         * 所有future執行完返回,否則阻塞
         * @Param
         * @author zqw
         */
        @Test
        public void testAllOf()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        System.out.println(1);
                        return "1";
                });
                CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
                        System.out.println(2);
                        return "2";
                });
                CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
                        System.out.println(3);
                        return "3";
                });
                CompletableFuture.allOf(future,future2,future3).get();
        }
        /**
         *有一個future執行完立即返回
         * @Param
         * @author zqw
         */
        @Test
        public void testAnyOf()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        try {
                                TimeUnit.SECONDS.sleep(300);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        System.out.println(1);
                        return "1";
                });
                CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
                        try {
                                TimeUnit.SECONDS.sleep(300);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        System.out.println(2);
                        return "2";
                });
                CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
                        System.out.println(3);
                        return "3";
                });
                CompletableFuture.anyOf(future,future2,future3).get();
        }
        /**
         *設成false話,不允許在線程運行時中斷,設成true的話就允許
         * 取消線程執行
         * @Param
         * @author zqw
         */
        @Test
        public void testCancel()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        try {
                                TimeUnit.SECONDS.sleep(300);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        System.out.println(1);
                        return "1";
                });
               future.cancel(false);
        }
        /**
         * 判斷線程執行是否被取消
         * @Param
         * @author zqw
         */
        @Test
        public void testIsCancelled()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        try {
                                TimeUnit.SECONDS.sleep(300);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                        System.out.println(1);
                        return "1";
                });
                future.cancel(false);
                System.out.println(future.isCancelled());
        }
        /**
         * 判斷線程是否被異常中斷,走了異常處理流程
         * @Param
         * @author zqw
         */
        @Test
        public void testIsCompletedExceptionally()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        if (true){
                                throw new NullPointerException("222");
                        }
                        System.out.println(1);
                        return "1";
                });
                System.out.println(future.exceptionally((s1) -> s1 + "23123").get());
                System.out.println(future.isCompletedExceptionally());
        }
        /**
         * 將線程執行結果設置爲指定值
         * @Param
         * @author zqw
         */
        @Test
        public void testObtrudeValue()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        System.out.println(1);
                        return "1";
                });
                System.out.println(future.get());
                future.obtrudeValue("123");
                System.out.println(future.get());
        }
        /**
         * 將線程執行結果轉換成指定異常
         * @Param
         * @author zqw
         */
        @Test
        public void testObtrudeException()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        System.out.println(1);
                        return "1";
                });
                System.out.println(future.get());
                future.obtrudeException(new Throwable("333"));
                System.out.println(future.get());
        }
        /**
         *全名@地址[完成狀態]
         * @Param
         * @author zqw
         */
        @Test
        public void testToString()throws Exception{
                CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                        System.out.println(1);
                        return "1";
                });
                System.out.println(future.toString());

        }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章