• CompletableFuture异步执行用法详解


    常用API

    thenAccept()

    thenAccept()用法:
    1. 此方法【没有返回值】
    2. 此方法需要借助【某一个】异步代码逻辑执行
    3. 此方法【可以拿到】被依赖的异步逻辑的返回值
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenAccept()
    5. 此方法不会造成阻塞
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    private static void thenAcceptTest() throws InterruptedException {
            CompletableFuture<String> futureA = CompletableFuture
            	.supplyAsync(() -> "task A");
            CompletableFuture<String> futureB = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task B";
            });
            // 放开此睡眠,thenAccept()中逻辑会正常执行
            // Thread.sleep(5000);
            futureB.thenAccept(b -> {
                System.out.println("run task C.");
                System.out.println("param:" + b);
            });
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    thenRun()

    thenRun()用法:
    1. 此方法【没有返回值】
    2. 此方法需要借助【某一个】异步代码逻辑执行
    3. 此方法【不能拿到】被依赖的异步逻辑的返回值
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenRun()
    5. 此方法不会造成阻塞
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void thenRunTest() throws InterruptedException {
            CompletableFuture<String> futureA = CompletableFuture
            	.supplyAsync(() -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task A";
            });
            // 放开此睡眠,thenRun()中逻辑会正常执行
            // Thread.sleep(5000);
            futureA.thenRun(() -> System.out.println("running task B"));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    thenApply()

    thenApply()用法:
    1. 此方法【有返回值】
    2. 此方法需要借助【某一个】异步代码逻辑执行
    3. 此方法【能拿到】被依赖的异步逻辑的返回值
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenApply()
    5. 此方法会造成阻塞
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void thenApplyTest() throws InterruptedException {
            CompletableFuture<String> futureA = CompletableFuture
            	.supplyAsync(() -> {
                a = a + 1;
                System.out.println(a + "1");
                return "task a ";
            });
            CompletableFuture<String> futureB = futureA.thenApply(s -> {
                // 下面睡眠解注释,会阻塞后续执行
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                a = a + 1;
                System.out.println(a + "2");
                return s + "and task b ";
            });
            CompletableFuture<String> future3 = futureB.thenApply(s -> {
                a = a + 1;
                System.out.println(a + "3");
                return s.toUpperCase();
            });
            System.out.println("4");
            System.out.println("a=" + a);
            // 输出最后异步任务返回值,会阻塞后续代码块
            // System.out.println(future3.join());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    thenCombine()

    thenCombine()用法,用于处理两个阶段的结果:
    1. 此方法【有返回值】
    2. 此方法需要借助【某两个】异步代码逻辑执行
    3. 此方法【能拿到】被依赖的异步逻辑的返回值
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenCombine()
    5. 此方法会造成后续代码阻塞
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void thenCombineTest() {
            CompletableFuture<String> taskA = CompletableFuture
            	.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            CompletableFuture<String> taskC = taskA.thenCombine(taskB, (a, b) -> {
                System.out.println("c:" + LocalDateTime.now());
                return (a + b).toUpperCase();
            });
            System.out.println(LocalDateTime.now());
            // System.out.println("数据合并:" + taskC.join());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    thenAcceptBoth()

    thenAcceptBoth()用法,用于处理两个阶段的结果:
    1. 此方法【无返回值】
    2. 此方法需要借助【某两个】异步代码逻辑执行
    3. 此方法【能拿到】被依赖的异步逻辑的返回值
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenAcceptBoth()
    5. 此方法【不会造成阻塞】
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void thenAcceptBothTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
    
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.thenAcceptBoth(taskB, (a, b) -> {
                System.out.println("c:" + LocalDateTime.now());
                System.out.println((a + b).toUpperCase());
            });
            System.out.println(LocalDateTime.now());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    runAfterBoth()

    runAfterBoth()用法,用于处理两个阶段的结果:
    1. 此方法【无返回值】
    2. 此方法需要借助【某两个】异步代码逻辑执行
    3. 此方法【不能拿到】被依赖的异步逻辑的返回值
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行runAfterBoth()
    5. 此方法【不会造成阻塞】
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void runAfterBothTest() {
            CompletableFuture<String> taskA = CompletableFuture
            	.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.runAfterBoth(taskB, () -> {
                System.out.println("c:" + LocalDateTime.now());
            });
            System.out.println(LocalDateTime.now());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    applyToEither()

    applyToEither()用法,用于处理两个阶段的结果:
    1. 此方法【有返回值】
    2. 此方法需要借助【某两个】异步代码逻辑执行
    3. 此方法【能返回】被依赖的异步逻辑中【最先拿到的返回值】
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行applyToEither()
    5. 此方法【不会造成阻塞】
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void applyToEitherTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            CompletableFuture<String> taskC = taskA.applyToEither(taskB, info -> {
                System.out.println("c:" + LocalDateTime.now());
                return info;
            });
            System.out.println(LocalDateTime.now());
            // 此方法阻塞后续代码
            // System.out.println("数据输出:" + taskC.join());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    acceptEither()

    acceptEither()用法,用于处理两个阶段的结果:
    1. 此方法【没有返回值】
    2. 此方法需要借助【某两个】异步代码逻辑执行
    3. 此方法【能返回】被依赖的异步逻辑中【最先拿到的返回值】
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行acceptEither()
    5. 此方法【不会造成阻塞】
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void acceptEitherTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.acceptEither(taskB, info -> {
                System.out.println("c:" + LocalDateTime.now());
                System.out.println(info);
            });
            System.out.println(LocalDateTime.now());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    runAfterEither()

    runAfterEither()用法,用于处理两个阶段的结果:
    1. 此方法【没有返回值】
    2. 此方法需要借助【某两个】异步代码逻辑执行
    3. 此方法【不能返回】被依赖的异步逻辑中【最先拿到的返回值】
    4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行runAfterEither()
    5. 此方法【不会造成阻塞】
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        private static void runAfterEitherTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.runAfterEither(taskB, () -> {
                System.out.println("c:" + LocalDateTime.now());
            });
            System.out.println(LocalDateTime.now());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    exceptionally()

    exceptionally()用法,用于处理异步逻辑中的异常:
    1. 此方法【有返回值】,即使异步逻辑没有返回值,也需要返回null
    2. 此方法接收Exception异常对象,并处理异常信息
    
    • 1
    • 2
    • 3
        private static void exceptionallyTest() {
            CompletableFuture<String> futureA = CompletableFuture.
                    supplyAsync(() -> "runA:" + (100 / 0))
                    .thenApply(s -> " resultA:" + s)
                    .exceptionally(e -> {
                        System.out.println(e.getMessage());
                        return "futureA result: errorA";
                    });
            CompletableFuture<String> futureB = CompletableFuture.
                    supplyAsync(() -> "runB:" + 50)
                    .thenApply(s -> "resultB:" + s)
                    .exceptionally(e -> "futureB result: errorB");
            System.out.println(futureA.join());// 报错进入exceptionally()处理
            System.out.println(futureB.join());// 未报错正常结束
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    whenComplete()

    whenComplete()用法,用于处理异步逻辑中的异常:
    1. 此方法【没有返回值】
    2. 此方法不管异步逻辑中是否有异常,都会走这个方法
    3. 此方法有两个接收值,一个接收【依赖异步逻辑的返回值】,一个接收【Exception异常】
    4. 两种顺序:
         4-1. 执行顺序 supplyAsync() -> thenApply() -> whenComplete() -> exceptionally()
             4-1-1. whenComplete()和exceptionally()都会接收到异常信息
             4-1-2. 最终join()返回的值是exceptionally()的返回值
         4-2. 执行顺序 supplyAsync() -> thenApply() -> exceptionally() -> whenComplete()
             4-2-1. exceptionally()会接收到异常信息,并处理后返回
             4-2-2. whenComplete()接收值是exceptionally()的返回值,接收的异常为null
             4-2-3. 最终join()返回的值是exceptionally()的返回值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
        private static void whenCompleteTest() {
            // 顺序一
            CompletableFuture<String> futureA = CompletableFuture.
                    supplyAsync(() -> "runA:" + (100 / 1))
                    .thenApply(s -> " resultA:" + (100 / 0) )
                    .whenComplete((s, e) -> {
                        if (s != null) {
                            System.out.println("whenComplete:" + s);//未执行
                        }
                        if (e == null) {
                            System.out.println("whenComplete:" + s);//未执行
                        } else {
                        	//java.lang.ArithmeticException: / by zero
                            System.out.println("whenComplete:" + e.getMessage());
                        }
                    })
                    .exceptionally(e -> {
                    	//ex:java.lang.ArithmeticException: / by zero
                        System.out.println("exceptionally():" + e.getMessage()); 
                        return "exceptionally() result";
                    });
            // 顺序二
            CompletableFuture<String> futureB = CompletableFuture.
                    supplyAsync(() -> "runA:" + (100 / 1))
                    .thenApply(s -> " resultA:" + (100 / 0) )
                    .exceptionally(e -> {
                    	//ex:java.lang.ArithmeticException: / by zero
                        System.out.println("exceptionally():" + e.getMessage()); 
                        return "exceptionally() result";
                    })
                    .whenComplete((s, e) -> {
                        if (e == null) {
                        	//whenComplete:exceptionally() result
                            System.out.println("whenComplete:" + s);
                        } else {
                        	//未执行
                            System.out.println("whenComplete:" + e.getMessage());
                        }
                    });
            //futureA.join():exceptionally() result
            System.out.println("futureA.join():" + futureA.join());
            //futureB.join():exceptionally() result
            System.out.println("futureB.join():" + futureB.join());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    handle()

    handle()用法,用于处理异步逻辑中的异常:
    1. 此方法【有返回值】
    2. 此方法不管异步逻辑中是否有异常,都会走这个方法
    3. 此方法有两个接收值,一个接收【依赖异步逻辑的返回值】,一个接收【Exception异常】
    4. 两种顺序:
         4-1. 执行顺序 supplyAsync() -> thenApply() -> handle() -> exceptionally()
             4-1-1. handle()会接收到异常信息,并进行处理后返回
             4-1-2. 最终join()返回的值是handle()的返回值
             4-1-3. 如果handle()也有异常,会进入exceptionally(),并处理异常返回,
             		最终join()返回的值是exceptionally()的返回值
         4-2. 执行顺序 supplyAsync() -> thenApply() -> exceptionally() -> handle()
             4-2-1. exceptionally()会接收到异常信息,并处理后返回
             4-2-2. handle()接收值是exceptionally()的返回值,接收的异常为null
             4-2-3. 最终join()返回的值是handle()的返回值
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
        private static void handleTest() {
            CompletableFuture<String> futureA = CompletableFuture.
                    supplyAsync(() -> "supplyAsync() result:" + (100 / 0))
                    .thenApply(s -> "thenApply() result:" + s)
                    .handle((s, e) -> {
                        if (e == null) {
                            System.out.println(s);//未执行
                        } else {
                        	//java.lang.ArithmeticException: / by zero
                            System.out.println(e.getMessage());
                        }
                        return "handle() result:" + (s == null ? "1000" : s);
                    })
                    .exceptionally(e -> {
                        System.out.println("ex:" + e.getMessage()); //未执行
                        return "futureA result exceptionally()";
                    });
            System.out.println(futureA.join());//handle() result:1000
    
            CompletableFuture<String> futureB = CompletableFuture.
                    supplyAsync(() -> "supplyAsync() result:" + (100 / 0))
                    .thenApply(s -> "thenApply() result:" + s)
                    .exceptionally(e -> {
                    	// java.lang.ArithmeticException: / by zero
                        System.out.println("ex:" + e.getMessage()); 
                        return "futureB result exceptionally()";
                    })
                    .handle((s, e) -> {
                        if (e == null) {
                            System.out.println(s);// futureB result exceptionally()
                        } else {
                            System.out.println(e.getMessage());// 未执行
                        }
                        return "handle() result:" + (s == null ? "1000" : s);
                    });
            //handle() result:futureB result exceptionally()
            System.out.println(futureB.join());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    CompletableFuture实现简单业务

    场景:异步执行逻辑,并每1秒循环获取返回值,直到异步执行结束返回结果

    1. join()方法和get()方法:会一直阻塞后续的代码执行
    2. 如果【join()方法和get()】在timer执行之前,会阻塞循环定时器的执行
    3. 在【join()方法和get()方法】以后的代码都会被阻塞
    4. get(long timeout, TimeUnit unit)方法:经过指定时间直接返回值,会阻塞后续代码一段时间
      但是要注意这个方法调用的位置,可以尝试将此方法放到循环定时器之前调用,会与现在结果大不一样
        private static void businessExample() throws ExecutionException,
        	 InterruptedException, TimeoutException {
            // 异步执行逻辑
            CompletableFuture<String> stringCompletableFuture1 = CompletableFuture
            	.supplyAsync(() -> {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 制造异常
                int a = 1 / 0;
                return "stringCompletableFuture1";
            }).exceptionally(e -> {
                // 如果逻辑执行有异常,进入此方法处理异常
                throw new RuntimeException();
            });
    
            // 循环定时获取结果,获取成功后关闭任务
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // 如果异步任务执行结束
                    if (stringCompletableFuture1.isDone()) {
                        // 关闭并清理任务
                        timer.cancel();
                        System.out.println("done = true,stop task total = " 
                        	+ timer.purge());
                        // 输出最终结果
                        System.out.println("getNow():" 
                        	+ LocalDateTime.now() + "   " 
                        	+ + stringCompletableFuture1.getNow(" END"));
                    } else {
                        System.out.println("getNow():" 
                        	+ LocalDateTime.now() + "   " 
                        	+ stringCompletableFuture1.getNow(" RUNNING"));
                    }
                }
            }, 0, 1000);
            System.out.println("get(time):" 
            	+ stringCompletableFuture1.get(1000L, TimeUnit.MILLISECONDS));
    
            // 代码块
            {
                System.out.println("join():" + stringCompletableFuture1.join());
                System.out.println("get():" + stringCompletableFuture1.get());
            }
    
    
            System.out.println(Thread.currentThread().getName() + "running");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    整体Demo(以上代码的合并版)

    import com.sun.deploy.util.StringUtils;
    
    import java.time.LocalDateTime;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    
    public class TestCompletableFuture {
        public static int a = 0;
    
        public static void main(String[] args) throws InterruptedException {
            // API实践
    //        thenAcceptTest();
    //        thenRunTest();
    //        thenApplyTest();
    //        thenCombineTest();
    //        thenAcceptBothTest();
    //        runAfterBothTest();
    //        applyToEitherTest();
    //        acceptEitherTest();
    //        runAfterEitherTest();
    //        exceptionallyTest();
    //        whenCompleteTest();
    //        handleTest();
            // 业务实例
    //        businessExample();
        }
    
    
        /**
         * handle()用法,用于处理异步逻辑中的异常
         * 1. 此方法【有返回值】
         * 2. 此方法不管异步逻辑中是否有异常,都会走这个方法
         * 3. 此方法有两个接收值,一个接收【依赖异步逻辑的返回值】,一个接收【Exception异常】
         * 4. 两种顺序:
         *      4-1. 执行顺序 supplyAsync() -> thenApply() -> handle() -> exceptionally()
         *          4-1-1. handle()会接收到异常信息,并进行处理后返回
         *          4-1-2. 最终join()返回的值是handle()的返回值
         *          4-1-3. 如果handle()也有异常,会进入exceptionally(),并处理异常返回,最终join()返回的值是exceptionally()的返回值
         *      4-2. 执行顺序 supplyAsync() -> thenApply() -> exceptionally() -> handle()
         *          4-2-1. exceptionally()会接收到异常信息,并处理后返回
         *          4-2-2. handle()接收值是exceptionally()的返回值,接收的异常为null
         *          4-2-3. 最终join()返回的值是handle()的返回值
         */
        private static void handleTest() {
            CompletableFuture<String> futureA = CompletableFuture.
                    supplyAsync(() -> "supplyAsync() result:" + (100 / 0))
                    .thenApply(s -> "thenApply() result:" + s)
                    .handle((s, e) -> {
                        if (e == null) {
                            System.out.println(s);//未执行
                        } else {
                            System.out.println(e.getMessage());//java.lang.ArithmeticException: / by zero
                        }
                        return "handle() result:" + (s == null ? "1000" : s);
                    })
                    .exceptionally(e -> {
                        System.out.println("ex:" + e.getMessage()); //未执行
                        return "futureA result exceptionally()";
                    });
            System.out.println(futureA.join());//handle() result:1000
    
            CompletableFuture<String> futureB = CompletableFuture.
                    supplyAsync(() -> "supplyAsync() result:" + (100 / 0))
                    .thenApply(s -> "thenApply() result:" + s)
                    .exceptionally(e -> {
                        System.out.println("ex:" + e.getMessage()); // java.lang.ArithmeticException: / by zero
                        return "futureB result exceptionally()";
                    })
                    .handle((s, e) -> {
                        if (e == null) {
                            System.out.println(s);// futureB result exceptionally()
                        } else {
                            System.out.println(e.getMessage());// 未执行
                        }
                        return "handle() result:" + (s == null ? "1000" : s);
                    });
            System.out.println(futureB.join());//handle() result:futureB result exceptionally()
        }
    
        /**
         * whenComplete()用法,用于处理异步逻辑中的异常
         * 1. 此方法【没有返回值】
         * 2. 此方法不管异步逻辑中是否有异常,都会走这个方法
         * 3. 此方法有两个接收值,一个接收【依赖异步逻辑的返回值】,一个接收【Exception异常】
         * 4. 两种顺序:
         *      4-1. 执行顺序 supplyAsync() -> thenApply() -> whenComplete() -> exceptionally()
         *          4-1-1. whenComplete()和exceptionally()都会接收到异常信息
         *          4-1-2. 最终join()返回的值是exceptionally()的返回值
         *      4-2. 执行顺序 supplyAsync() -> thenApply() -> exceptionally() -> whenComplete()
         *          4-2-1. exceptionally()会接收到异常信息,并处理后返回
         *          4-2-2. whenComplete()接收值是exceptionally()的返回值,接收的异常为null
         *          4-2-3. 最终join()返回的值是exceptionally()的返回值
         */
        private static void whenCompleteTest() {
            // 顺序一
            CompletableFuture<String> futureA = CompletableFuture.
                    supplyAsync(() -> "runA:" + (100 / 1))
                    .thenApply(s -> " resultA:" + (100 / 0) )
                    .whenComplete((s, e) -> {
                        if (s != null) {
                            System.out.println("whenComplete:" + s);//未执行
                        }
                        if (e == null) {
                            System.out.println("whenComplete:" + s);//未执行
                        } else {
                            System.out.println("whenComplete:" + e.getMessage());//java.lang.ArithmeticException: / by zero
                        }
                    })
                    .exceptionally(e -> {
                        System.out.println("exceptionally():" + e.getMessage()); //ex:java.lang.ArithmeticException: / by zero
                        return "exceptionally() result";
                    });
            // 顺序二
            CompletableFuture<String> futureB = CompletableFuture.
                    supplyAsync(() -> "runA:" + (100 / 1))
                    .thenApply(s -> " resultA:" + (100 / 0) )
                    .exceptionally(e -> {
                        System.out.println("exceptionally():" + e.getMessage()); //ex:java.lang.ArithmeticException: / by zero
                        return "exceptionally() result";
                    })
                    .whenComplete((s, e) -> {
                        if (e == null) {
                            System.out.println("whenComplete:" + s);//whenComplete:exceptionally() result
                        } else {
                            System.out.println("whenComplete:" + e.getMessage());//未执行
                        }
                    });
            System.out.println("futureA.join():" + futureA.join());//futureA.join():exceptionally() result
            System.out.println("futureB.join():" + futureB.join());//futureB.join():exceptionally() result
        }
    
        /**
         * exceptionally()用法,用于处理异步逻辑中的异常
         * 1. 此方法【有返回值】,即使异步逻辑没有返回值,也需要返回null
         * 2. 此方法接收Exception异常对象,并处理异常信息
         */
        private static void exceptionallyTest() {
            CompletableFuture<String> futureA = CompletableFuture.
                    supplyAsync(() -> "runA:" + (100 / 0))
                    .thenApply(s -> " resultA:" + s)
                    .exceptionally(e -> {
                        System.out.println(e.getMessage());
                        return "futureA result: errorA";
                    });
            CompletableFuture<String> futureB = CompletableFuture.
                    supplyAsync(() -> "runB:" + 50)
                    .thenApply(s -> "resultB:" + s)
                    .exceptionally(e -> "futureB result: errorB");
            System.out.println(futureA.join());// 报错进入exceptionally()处理
            System.out.println(futureB.join());// 未报错正常结束
        }
    
        /**
         * runAfterEither()用法,用于处理两个阶段的结果
         * 1. 此方法【没有返回值】
         * 2. 此方法需要借助【某两个】异步代码逻辑执行
         * 3. 此方法【不能返回】被依赖的异步逻辑中【最先拿到的返回值】
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行runAfterEither()
         * 5. 此方法【不会造成阻塞】
         */
        private static void runAfterEitherTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.runAfterEither(taskB, () -> {
                System.out.println("c:" + LocalDateTime.now());
            });
            System.out.println(LocalDateTime.now());
        }
    
        /**
         * acceptEither()用法,用于处理两个阶段的结果
         * 1. 此方法【没有返回值】
         * 2. 此方法需要借助【某两个】异步代码逻辑执行
         * 3. 此方法【能返回】被依赖的异步逻辑中【最先拿到的返回值】
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行acceptEither()
         * 5. 此方法【不会造成阻塞】
         */
        private static void acceptEitherTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.acceptEither(taskB, info -> {
                System.out.println("c:" + LocalDateTime.now());
                System.out.println(info);
            });
            System.out.println(LocalDateTime.now());
        }
    
        /**
         * applyToEither()用法,用于处理两个阶段的结果
         * 1. 此方法【有返回值】
         * 2. 此方法需要借助【某两个】异步代码逻辑执行
         * 3. 此方法【能返回】被依赖的异步逻辑中【最先拿到的返回值】
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行applyToEither()
         * 5. 此方法【不会造成阻塞】
         */
        private static void applyToEitherTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            CompletableFuture<String> taskC = taskA.applyToEither(taskB, info -> {
                System.out.println("c:" + LocalDateTime.now());
                return info;
            });
            System.out.println(LocalDateTime.now());
            // 此方法阻塞后续代码
    //        System.out.println("数据输出:" + taskC.join());
        }
    
        /**
         * runAfterBoth()用法,用于处理两个阶段的结果
         * 1. 此方法【无返回值】
         * 2. 此方法需要借助【某两个】异步代码逻辑执行
         * 3. 此方法【不能拿到】被依赖的异步逻辑的返回值
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行runAfterBoth()
         * 5. 此方法【不会造成阻塞】
         */
        private static void runAfterBothTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.runAfterBoth(taskB, () -> {
                System.out.println("c:" + LocalDateTime.now());
            });
            System.out.println(LocalDateTime.now());
        }
    
        /**
         * thenAcceptBoth()用法,用于处理两个阶段的结果
         * 1. 此方法【无返回值】
         * 2. 此方法需要借助【某两个】异步代码逻辑执行
         * 3. 此方法【能拿到】被依赖的异步逻辑的返回值
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenAcceptBoth()
         * 5. 此方法【不会造成阻塞】
         */
        private static void thenAcceptBothTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
    
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            taskA.thenAcceptBoth(taskB, (a, b) -> {
                System.out.println("c:" + LocalDateTime.now());
                System.out.println((a + b).toUpperCase());
            });
            System.out.println(LocalDateTime.now());
        }
    
        /**
         * thenCombine()用法,用于处理两个阶段的结果
         * 1. 此方法【有返回值】
         * 2. 此方法需要借助【某两个】异步代码逻辑执行
         * 3. 此方法【能拿到】被依赖的异步逻辑的返回值
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenCombine()
         * 5. 此方法会造成后续代码阻塞
         */
        private static void thenCombineTest() {
            CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
                System.out.println("a:" + LocalDateTime.now());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task a ";
            });
            CompletableFuture<String> taskB = CompletableFuture.supplyAsync(() -> {
                System.out.println("b:" + LocalDateTime.now());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task b ";
            });
            CompletableFuture<String> taskC = taskA.thenCombine(taskB, (a, b) -> {
                System.out.println("c:" + LocalDateTime.now());
                return (a + b).toUpperCase();
            });
            System.out.println(LocalDateTime.now());
    //        System.out.println("数据合并:" + taskC.join());
        }
    
        /**
         * thenApply()用法
         * 1. 此方法【有返回值】
         * 2. 此方法需要借助【某一个】异步代码逻辑执行
         * 3. 此方法【能拿到】被依赖的异步逻辑的返回值
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenApply()
         * 5. 此方法会造成阻塞
         */
        private static void thenApplyTest() throws InterruptedException {
            CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> {
                a = a + 1;
                System.out.println(a + "1");
                return "task a ";
            });
            CompletableFuture<String> futureB = futureA.thenApply(s -> {
                // 下面睡眠解注释,会阻塞后续执行
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                a = a + 1;
                System.out.println(a + "2");
                return s + "and task b ";
            });
            CompletableFuture<String> future3 = futureB.thenApply(s -> {
                a = a + 1;
                System.out.println(a + "3");
                return s.toUpperCase();
            });
            System.out.println("4");
            System.out.println("a=" + a);
            // 输出最后异步任务返回值,会阻塞后续代码块
    //        System.out.println(future3.join());
        }
    
        /**
         * thenRun()用法
         * 1. 此方法【没有返回值】
         * 2. 此方法需要借助【某一个】异步代码逻辑执行
         * 3. 此方法【不能拿到】被依赖的异步逻辑的返回值
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenRun()
         * 5. 此方法不会造成阻塞
         */
        private static void thenRunTest() throws InterruptedException {
            CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task A";
            });
            // 放开此睡眠,thenRun()中逻辑会正常执行
    //        Thread.sleep(5000);
            futureA.thenRun(() -> System.out.println("running task B"));
        }
    
        /**
         * thenAccept()用法
         * 1. 此方法【没有返回值】
         * 2. 此方法需要借助【某一个】异步代码逻辑执行
         * 3. 此方法【可以拿到】被依赖的异步逻辑的返回值
         * 4. 如果被依赖的异步逻辑没有执行完,不会等待,且不会执行thenAccept()
         * 5. 此方法不会造成阻塞
         */
        private static void thenAcceptTest() throws InterruptedException {
            CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> "task A");
            CompletableFuture<String> futureB = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "task B";
            });
            // 放开此睡眠,thenAccept()中逻辑会正常执行
    //        Thread.sleep(5000);
            futureB.thenAccept(b -> {
                System.out.println("run task C.");
                System.out.println("param:" + b);
            });
        }
    
        /**
         * 场景:异步执行逻辑,并每1秒循环获取返回值,直到异步执行结束返回结果
         * 1. join()方法和get()方法:会一直阻塞后续的代码执行
         * 2. 如果【join()方法和get()】在timer执行之前,会阻塞循环定时器的执行,所以说在【join()方法和get()方法】以后的代码都会被阻塞
         * 3. get(long timeout, TimeUnit unit)方法:经过指定时间直接返回值,会阻塞后续代码一段时间,但是要注意这个方法调用的位置,可以尝试将此方法放到循环定时器之前调用,会与现在结果大不一样
         */
        private static void businessExample() throws ExecutionException, InterruptedException, TimeoutException {
            // 异步执行逻辑
            CompletableFuture<String> stringCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 制造异常
                int a = 1 / 0;
                return "stringCompletableFuture1";
            }).exceptionally(e -> {
                // 如果逻辑执行有异常,进入此方法处理异常
                throw new RuntimeException();
            });
    
            // 循环定时获取结果,获取成功后关闭任务
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // 如果异步任务执行结束
                    if (stringCompletableFuture1.isDone()) {
                        // 关闭并清理任务
                        timer.cancel();
                        System.out.println("done = true,stop task total = " + timer.purge());
                        // 输出最终结果
                        System.out.println("getNow():" + LocalDateTime.now() + "   " + stringCompletableFuture1.getNow(" END"));
                    } else {
                        System.out.println("getNow():" + LocalDateTime.now() + "   " + stringCompletableFuture1.getNow(" RUNNING"));
                    }
                }
            }, 0, 1000);
            System.out.println("get(time):" + stringCompletableFuture1.get(1000L, TimeUnit.MILLISECONDS));
    
            // 代码块
            {
                System.out.println("join():" + stringCompletableFuture1.join());
                System.out.println("get():" + stringCompletableFuture1.get());
            }
    
    
            System.out.println(Thread.currentThread().getName() + "running");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
  • 相关阅读:
    64 ---- 两直线的位置关系
    SpringMVC ---- HelloWorld
    ChatGPT帮助工程师写代码:从功能模块完善到成功执行,实现需求
    kicad源代码研究:symbol properties窗口中为SCH_SYMBOL添加或删除一个sch_field
    【数据库系统概论】数据库的四个基本概念:数据、数据库、数据库管理系统和数据库系统
    MFC框架程序解析
    如何使用 PHP PDO 计算 MySQL 表中的行数
    Mysql索引优化实战
    如何获取高质量的微信私域客户?
    mysql buffer pool详解
  • 原文地址:https://blog.csdn.net/m0_63164811/article/details/126642235