CompletableFuture异步计算结果(2) 按顺序执行异步任务

符号 阅读:684 2021-04-01 11:02:37 评论:0

1.thenAccept方法,前置子线程运行成功才执行,能获取前一个任务结果,无返回结果
在这里插入图片描述
thenAccept 前置执行成功,后续仍使用前置的子线程执行,无返回结果
thenAcceptAsync一个参数方法 前置执行成功,后续使用守护线程执行,无返回结果
thenAcceptAsync两个参数方法 前置执行成功,后续使用新的自定义线程执行,无返回结果
1.1 thenAccept 测试

public class ThenAcceptTest { 
    public static void main(String[] args) { 
        System.out.println("主线程"+Thread.currentThread().getName()); 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture.supplyAsync(()->{ 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程"+Thread.currentThread().getName()); 
            return "123"; 
        },executorService).thenAccept(value-> { 
            System.out.println(value + "子线程" + Thread.currentThread().getName()); 
            executorService.shutdown(); 
        }); 
        System.out.println("主线程结束"+Thread.currentThread().getName()); 
    } 
} 

在这里插入图片描述

1.2 thenAcceptAsync 一个参数方法测试

public class ThenAcceptTest { 
    public static void main(String[] args) { 
        System.out.println("主线程"+Thread.currentThread().getName()); 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture.supplyAsync(()->{ 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程"+Thread.currentThread().getName()); 
            return "123"; 
        },executorService).thenAcceptAsync(value-> { 
            System.out.println(value + "子线程" + Thread.currentThread().getName()); 
            executorService.shutdown(); 
        }); 
        System.out.println("主线程结束"+Thread.currentThread().getName()); 
    } 
} 

thenAcceptAsync
1.3 thenAcceptAsync 两个参数方法测试

public class ThenAcceptTest { 
    public static void main(String[] args) { 
        System.out.println("主线程"+Thread.currentThread().getName()); 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture.supplyAsync(()->{ 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程"+Thread.currentThread().getName()); 
            return "123"; 
        },executorService).thenAcceptAsync(value-> { 
            System.out.println(value + "子线程" + Thread.currentThread().getName()); 
            executorService.shutdown(); 
        },executorService); 
        System.out.println("主线程结束"+Thread.currentThread().getName()); 
    } 
} 

在这里插入图片描述
2.thenApply方法,前置子线程运行成功才执行,能获取前一个任务结果,有返回结果
在这里插入图片描述
thenApply 前置执行成功,后续仍使用前置的子线程执行,有返回结果
thenApplyAsync 一个参数方法 前置执行成功,后续使用守护线程执行,有返回结果
thenApplyAsync 两个参数方法 前置执行成功,后续使用新的自定义线程执行,有返回结果
2.1 thenApply 测试

public class ThenApplyTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程" + Thread.currentThread().getName()); 
            return "123"; 
        }, executorService).thenApply(value -> { 
            System.out.println("thenApply子线程" + Thread.currentThread().getName()); 
            executorService.shutdown(); 
            return value + "thenApply"; 
        }); 
        String s = null; 
        try { 
            s = future.get(); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } catch (ExecutionException e) { 
            e.printStackTrace(); 
        } 
        System.out.println(s+"主线程结束" + Thread.currentThread().getName()); 
    } 
} 

在这里插入图片描述
2.2 thenApplyAsync 一个参数方法测试

public class ThenApplyTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程" + Thread.currentThread().getName()); 
            return "123"; 
        }, executorService).thenApplyAsync(value -> { 
            System.out.println("thenApply子线程" + Thread.currentThread().getName()); 
            executorService.shutdown(); 
            return value + "thenApply"; 
        }); 
        String s = null; 
        try { 
            s = future.get(); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } catch (ExecutionException e) { 
            e.printStackTrace(); 
        } 
        System.out.println(s+"主线程结束" + Thread.currentThread().getName()); 
    } 
} 

在这里插入图片描述
2.3 thenApplyAsync 两个参数方法测试

public class ThenApplyTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程" + Thread.currentThread().getName()); 
            return "123"; 
        }, executorService).thenApplyAsync(value -> { 
            System.out.println("thenApply子线程" + Thread.currentThread().getName()); 
            executorService.shutdown(); 
            return value + "thenApply"; 
        },executorService); 
        String s = null; 
        try { 
            s = future.get(); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } catch (ExecutionException e) { 
            e.printStackTrace(); 
        } 
        System.out.println(s+"主线程结束" + Thread.currentThread().getName()); 
    } 
} 

在这里插入图片描述
3.thenRun方法,前置子线程运行成功才执行,不能获取前一个任务结果,无返回结果
在这里插入图片描述
thenRun: 前置执行成功,后续仍使用前置的子线程执行,不能获取前置返回的结果,无返回结果
thenRunAsync:一个参数方法,前置执行成功,后续使用守护线程执行,不能获取前置返回的结果,无返回结果
thenRunAsync: 两个参数方法前置执行成功,后续使用新的自定义线程执行,不能获取前置返回的结果,无返回结果
3.1 thenRun测试

public class ThenRunTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> { 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程:" + Thread.currentThread().getName()); 
        }, executorService).thenRun(() -> { 
            System.out.println("后续子线程:" + Thread.currentThread().getName()); 
        }); 
 
        System.out.println("主线程结束:"+Thread.currentThread().getName()); 
        executorService.shutdown(); 
    } 
} 

在这里插入图片描述
3.2 thenRunAsync 一个参数的方法 测试

public class ThenRunTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> { 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程:" + Thread.currentThread().getName()); 
        }, executorService).thenRunAsync(() -> { 
            System.out.println("后续子线程:" + Thread.currentThread().getName()); 
        }); 
 
        System.out.println("主线程结束:"+Thread.currentThread().getName()); 
        executorService.shutdown(); 
    } 
} 

在这里插入图片描述
3.2 thenRunAsync 两个参数的方法 测试

public class ThenRunTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> { 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("子线程:" + Thread.currentThread().getName()); 
        }, executorService).thenRunAsync(() -> { 
            System.out.println("后续子线程:" + Thread.currentThread().getName()); 
            executorService.shutdown(); 
        },executorService); 
 
        System.out.println("主线程结束:"+Thread.currentThread().getName()); 
    } 
} 

在这里插入图片描述
4.whenComplete方法,无论前置方法是否异常,均调用此方法,入参为前置结果和异常信息,返回结果为前置方法的返回结果
在这里插入图片描述

public class WhenCompleteTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("前置程序子线程:" + Thread.currentThread().getName()); 
            return "123"; 
        }, executorService).whenComplete((v, e) -> { 
            if (e == null) { 
                System.out.println(v + "无异常:" + Thread.currentThread().getName()); 
            } else { 
                System.out.println(v + "有异常:" + Thread.currentThread().getName()); 
            } 
            System.out.println("后续程序子线程2:" + Thread.currentThread().getName()); 
            v = v + "456"; 
            System.out.println(v); 
        }); 
        try { 
            String s = future.get(); 
            System.out.println("执行结果:" + s); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        System.out.println("主线程结束:" + Thread.currentThread().getName()); 
        executorService.shutdown(); 
    } 
} 

在这里插入图片描述
如果前置抛异常,后续接受到的结果为null,和异常信息,主程序future.get()时会捕获异常

public class WhenCompleteTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { 
            int i = 1/0; 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("前置程序子线程:" + Thread.currentThread().getName()); 
            return "123"; 
        }, executorService).whenCompleteAsync((v, e) -> { 
            if (e == null) { 
                System.out.println(v + "无异常:" + Thread.currentThread().getName()); 
            } else { 
                System.out.println(v + "有异常:" + Thread.currentThread().getName()); 
            } 
            System.out.println("后续程序子线程2:" + Thread.currentThread().getName()); 
            v = v + "456"; 
            System.out.println(v); 
        },executorService); 
        try { 
            String s = future.get(); 
            System.out.println("执行结果:" + s); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        System.out.println("主线程结束:" + Thread.currentThread().getName()); 
        executorService.shutdown(); 
    } 
} 

在这里插入图片描述
5.handle方法,无论前置方法是否异常,均调用此方法,入参为前置结果和异常信息,返回结果为后续方法的返回结果
在这里插入图片描述

public class HandleTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { 
        	//模拟异常 
            //int i = 1 / 0; 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("前置程序子线程:" + Thread.currentThread().getName()); 
            return "123"; 
        }, executorService).handle((v, e) -> { 
            if (e == null) { 
                System.out.println(v + "无异常:" + Thread.currentThread().getName()); 
            } else { 
                System.out.println(v + "有异常:" + Thread.currentThread().getName()); 
            } 
            System.out.println("后续程序子线程2:" + Thread.currentThread().getName()); 
            v = v + "456"; 
            System.out.println(v); 
            return v; 
        }); 
        try { 
            String s = future.get(); 
            System.out.println("执行结果:" + s); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        System.out.println("主线程结束:" + Thread.currentThread().getName()); 
        executorService.shutdown(); 
    } 
} 

在这里插入图片描述
如果前置方法抛出异常,则后续方法入参为null和异常信息,主程序future.get()不抛出异常
在这里插入图片描述
6.exceptionally方法,前置方法异常,调用此方法,入参为前置异常信息,返回结果为后续方法的返回结果

public class ExceptionallyTest { 
    public static void main(String[] args) { 
        ExecutorService executorService = Executors.newFixedThreadPool(100); 
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { 
            int i = 1 / 0; 
            try { 
                TimeUnit.SECONDS.sleep(3); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println("前置程序子线程:" + Thread.currentThread().getName()); 
            return "123"; 
        }, executorService).exceptionally(e -> { 
            System.out.println(e.getMessage()); 
            System.out.println("异常子线程:" + Thread.currentThread().getName()); 
            return e.getMessage(); 
        }); 
        try { 
            String s = future.get(); 
            System.out.println("执行结果:" + s); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        System.out.println("主线程结束:" + Thread.currentThread().getName()); 
        executorService.shutdown(); 
    } 
} 

在这里插入图片描述

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号