古交市网站建设公司邯郸网站建设推荐咨询

diannao/2026/1/21 2:36:48/文章来源:
古交市网站建设公司,邯郸网站建设推荐咨询,更改wordpress后台登录地址,泊头做网站的有哪些1、前言 学习java基础时候多线程使用我们首先学习的 Runable 、Future 、 Thread 、ExecutorService、Callable等相关类#xff0c;在我们日常工作或者学习中有些场景并不满足我们需求#xff0c;JDK8引入了一个新的类 CompletableFuture 来解决之前得问题#xff0c; Comp…1、前言 学习java基础时候多线程使用我们首先学习的 Runable 、Future 、 Thread 、ExecutorService、Callable等相关类在我们日常工作或者学习中有些场景并不满足我们需求JDK8引入了一个新的类 CompletableFuture 来解决之前得问题 CompletableFuture 实现了 Future 接口和 CompletionStage 。因此 CompletableFuture是对 Futrue的功能增强包含了Future的功能。从继承的另一个 CompletionStage 的名称来看完成阶段性的接口接下来了解一下 CompletableFuture 的一些基本情况以及使用和注意事项。 2、CompletableFuture使用 CompletableFuture提供了几十种方法辅助我们的异步任务场景。这些方法包括创建异步任务、任务异步回调、多个任务组合处理等方面JDK8设计出CompletableFuture。CompletableFuture提供了一种观察者模式类似的机制可以让任务执行完成后通知监听的一方。 自定义线城池一下代码都会使用到 static ExecutorService executor Executors.newFixedThreadPool(3, new ThreadFactory() {int count 1;Overridepublic Thread newThread(Runnable runnable) {return new Thread(runnable, completableFuture-executor- count);}});2.1创建异步任务 runAsync 用于构建一个没有入参也没有出参的任务 supplyAsync 用于构建一个没有入参但是有出参的任务 runAsync和supplyAsync可以指定线程池如果不指定则使用ForkJoinPool的commonPool线程池 先看源码 public static U CompletableFutureU supplyAsync(SupplierU supplier) {return asyncSupplyStage(asyncPool, supplier);}public static U CompletableFutureU supplyAsync(SupplierU supplier,Executor executor) {return asyncSupplyStage(screenExecutor(executor), supplier);}public static CompletableFutureVoid runAsync(Runnable runnable) {return asyncRunStage(asyncPool, runnable);}public static CompletableFutureVoid runAsync(Runnable runnable,Executor executor) {return asyncRunStage(screenExecutor(executor), runnable);}runAsync执行CompletableFuture任务没有返回值。 /*** runAsync执行CompletableFuture任务没有返回值。*/public static void runAsyncExample() throws ExecutionException, InterruptedException {CompletableFutureVoid completableFuture CompletableFuture.runAsync(() - {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束);});System.out.println(异步线程执行状态 completableFuture.isDone());System.out.println(主线程正在执行);System.out.println(异步线程执行状态 completableFuture.isDone());System.out.println(completableFuture.get());}执行结果 异步线程执行状态false completableFuture-当前线程名称ForkJoinPool.commonPool-worker-9 异步线程执行执行结束 nullsupplyAsync执行CompletableFuture任务支持返回值-使用自定义线城池 public static void supplyAsyncExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName());System.out.println(异步线程执行执行结束);return msg-异步线程执行结束;}, executor);System.out.println(主线程-------supplyAsyncExample-----正在执行);System.out.println(completableFuture.join());executor.shutdown(); // 线程池需要关闭}执行结果 主线程-------supplyAsyncExample-----正在执行 completableFuture2-当前线程名称completableFuture-executor-1 异步线程执行执行结束 msg-异步线程执行结束2.2任务异步回调 2.2.1 thenRun/thenRunAsync 不关心上一个任务的执行返回结果无参数无返回值 CompletableFuture的thenRun方法通俗点讲就是做完第一个任务后再做第二个任务。某个任务执行完成后执行回调方法但是前后两个任务没有参数传递第二个任务也没有返回值 如果你执行第一个任务的时候传入了一个自定义线程池 调用thenRun方法执行第二个任务时则第二个任务和第一个任务是共用同一个线程池。调用thenRunAsync执行第二个任务时则第一个任务使用的是你自己传入的线程池第二个任务使用的是ForkJoin线程池 public CompletableFutureVoid thenRun(Runnable action); public CompletableFutureVoid thenRunAsync(Runnable action);thenRun代码 public static void thenRunExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束当前线程Thread.currentThread().getName());return msg-异步线程执行结束;}, executor);System.out.println(主线程-------thenRunExample-----正在执行1);CompletableFutureVoid voidCompletableFuture completableFuture.thenRun(() - {System.out.println(thenRunExample----正在执行第二个任务当前线程Thread.currentThread().getName());});System.out.println(主线程-------thenRunExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线程池需要关闭}执行结果 主线程-------thenRunExample-----正在执行1 主线程-------thenRunExample-----正在执行2 异步线程执行执行结束当前线程completableFuture-executor-1 thenRunExample----正在执行第二个任务当前线程completableFuture-executor-1 nullthenRunAsync代码-异步使用自定义线城池 public static void thenRunAsyncExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束---当前线城Thread.currentThread().getName());return msg-异步线程执行结束;}, executor);System.out.println(主线程-------thenRunAsyncExample-----正在执行1);CompletableFutureVoid voidCompletableFuture completableFuture.thenRunAsync(() - {System.out.println(thenRunExample----正在执行第二个任务---当前线城Thread.currentThread().getName());});System.out.println(主线程-------thenRunAsyncExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线程池需要关闭}执行结果 主线程-------thenRunAsyncExample-----正在执行1 主线程-------thenRunAsyncExample-----正在执行2 异步线程执行执行结束---当前线城completableFuture-executor-1 thenRunExample----正在执行第二个任务---当前线城ForkJoinPool.commonPool-worker-9 null2.2.2.thenAccept/thenAcceptAsync 第一个任务执行完成后执行第二个回调方法任务会将该任务的执行结果作为入参传递到回调方法中但是回调方法是没有返回值的。 thenAccept代码示例 public static void thenAcceptExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束--执行线程 Thread.currentThread().getName());return msg-异步线程执行结束;}, executor);System.out.println(主线程-------thenAcceptExample-----正在执行1);CompletableFutureVoid voidCompletableFuture completableFuture.thenAccept((msg) - {System.out.println(thenAccept接受上一步返回参数 msg);System.out.println(thenAcceptExample----正在执行第二个任务--执行线程 Thread.currentThread().getName());});System.out.println(主线程-------thenAcceptExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线程池需要关闭}执行结果 主线程-------thenAcceptExample-----正在执行1 主线程-------thenAcceptExample-----正在执行2 异步线程执行执行结束--执行线程completableFuture-executor-1 thenAccept接受上一步返回参数msg-异步线程执行结束 thenAcceptExample----正在执行第二个任务--执行线程completableFuture-executor-1 nullthenAcceptAsync代码示例 public static void thenAcceptAsyncExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束---执行线程 Thread.currentThread().getName());return msg-异步线程执行结束;}, executor);System.out.println(主线程-------thenAcceptExample-----正在执行1);CompletableFutureVoid voidCompletableFuture completableFuture.thenAcceptAsync((msg) - {System.out.println(thenAccept接受上一步返回参数 msg);System.out.println(thenAcceptExample----正在执行第二个任务---执行线程 Thread.currentThread().getName());}, executor);System.out.println(主线程-------thenAcceptExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线程池需要关闭}运行结果 主线程-------thenAcceptExample-----正在执行1 主线程-------thenAcceptExample-----正在执行2 异步线程执行执行结束---执行线程completableFuture-executor-1 thenAccept接受上一步返回参数msg-异步线程执行结束 thenAcceptExample----正在执行第二个任务---执行线程completableFuture-executor-2 null2.2.3 thenApply/thenApplyAsync 第一个任务执行完成后执行第二个回调方法任务会将该任务的执行结果作为入参传递到回调方法中并且回调方法是有返回值的 thenApply-不关心上一个任务的执行返回结果有参数有返回值- 代码 public static void thenApplyExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束---当前线程 Thread.currentThread().getName());return msg-异步线程执行结束;}, executor);System.out.println(主线程-------thenApplyExample-----正在执行1);CompletableFutureString voidCompletableFuture completableFuture.thenApply((msg) - {System.out.println(thenApply接受上一步返回参数 msg);System.out.println(thenApplyExample----正在执行第二个任务----当前线程 Thread.currentThread().getName());return thenApplyExample执行任务结束;});System.out.println(主线程-------thenApplyExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线池需要关闭}执行结果 主线程-------thenApplyExample-----正在执行1 主线程-------thenApplyExample-----正在执行2 异步线程执行执行结束---当前线程completableFuture-executor-1 thenApply接受上一步返回参数msg-异步线程执行结束 thenApplyExample----正在执行第二个任务----当前线程completableFuture-executor-1 thenApplyExample执行任务结束thenApplyAsync-任务异步回调-不关心上一个任务的执行返回结果有参数有返回值- public static void thenApplyAsyncExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束);System.out.println(当前线程名称 Thread.currentThread().getName());return msg-异步线程执行结束;}, executor);System.out.println(主线程-------thenApplyAsyncExample-----正在执行1);CompletableFutureString voidCompletableFuture completableFuture.thenApplyAsync((msg) - {System.out.println(thenApply接受上一步返回参数 msg);System.out.println(当前线程名称 Thread.currentThread().getName());System.out.println(thenApplyAsyncExample----正在执行第二个任务);return thenApplyAsyncExample执行任务结束;});System.out.println(主线程-------thenApplyAsyncExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线池需要关闭}执行结果 主线程-------thenApplyAsyncExample-----正在执行1 主线程-------thenApplyAsyncExample-----正在执行2 异步线程执行执行结束 当前线程名称completableFuture-executor-1 thenApply接受上一步返回参数msg-异步线程执行结束 当前线程名称ForkJoinPool.commonPool-worker-9 thenApplyAsyncExample----正在执行第二个任务 thenApplyAsyncExample执行任务结束2.2.4 exceptionally- 某个任务执行异常时执行的回调方法;并且有抛出异常作为参数传递到回调方法。 执行代码 public static void exceptionallyExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(当前线程名称 Thread.currentThread().getName());throw new RuntimeException(咳咳执行异常啦);}, executor);System.out.println(主线程-------exceptionallyExample-----正在执行1);CompletableFutureString voidCompletableFuture completableFuture.exceptionally((throwable) - {System.out.println(当前线程名称 Thread.currentThread().getName());System.out.println(exceptionallyExample----发生异常信息---- throwable.getMessage());return handleExample执行结束;});System.out.println(主线程-------exceptionallyExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线池需关闭}执行结果 主线程-------exceptionallyExample-----正在执行1 主线程-------exceptionallyExample-----正在执行2 当前线程名称completableFuture-executor-1 当前线程名称completableFuture-executor-1 exceptionallyExample----发生异常信息----java.lang.RuntimeException: 咳咳执行异常啦 handleExample执行结束2.2.5whenComplete/handle whenComplete:某个任务执行完成后执行的回调方法无返回值并且whenComplete方法返回的CompletableFuture的result是上个任务的结果。 handle:任务执行完成后执行回调方法并且是有返回值的;并且handle方法返回的CompletableFuture的result是回调方法执行的结果。 whenComplete 代码示例 public static void whenCompleteExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束--当前线程名称 Thread.currentThread().getName());return msg-异步线程执行结束666;}, executor);System.out.println(主线程-------whenCompleteExample-----正在执行1);CompletableFutureString voidCompletableFuture completableFuture.whenComplete((msg, throwable) - {System.out.println(当前线程名称 Thread.currentThread().getName());System.out.println(whenCompleteExample----接受参数 msg);});System.out.println(主线程-------whenCompleteExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线池需要关闭}执行结果 主线程-------whenCompleteExample-----正在执行1 主线程-------whenCompleteExample-----正在执行2 异步线程执行执行结束--当前线程名称completableFuture-executor-1 当前线程名称completableFuture-executor-1 whenCompleteExample----接受参数msg-异步线程执行结束666 msg-异步线程执行结束666 Handle 代码示例 public static void handleExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(异步线程执行执行结束);System.out.println(当前线程名称 Thread.currentThread().getName());return msg-异步线程执行结束666;}, executor);System.out.println(主线程-------handleExample-----正在执行1);CompletableFutureString voidCompletableFuture completableFuture.handle((msg, throwable) - {System.out.println(当前线程名称 Thread.currentThread().getName());System.out.println(handleExample----接受参数 msg);return handleExample执行结束;});System.out.println(主线程-------handleExample-----正在执行2);System.out.println(voidCompletableFuture.get());executor.shutdown(); // 线池需要关闭}执行结果 主线程-------handleExample-----正在执行1 主线程-------handleExample-----正在执行2 异步线程执行执行结束 当前线程名称completableFuture-executor-1 当前线程名称completableFuture-executor-1 handleExample----接受参数msg-异步线程执行结束666 handleExample执行结束2.3 多个任务组合处理 2.3.1 thenCombine / thenAcceptBoth / runAfterBoth----AND组合关系 将两个CompletableFuture组合起来只有这两个都正常执行完了才会执行某个任务。 区别在于 thenCombine会将两个任务的执行结果作为方法入参传递到指定方法中且有返回值 thenAcceptBoth: 会将两个任务的执行结果作为方法入参传递到指定方法中且无返回值 runAfterBoth 不会把执行结果当做方法入参且没有返回值。 thenCombine代码示列 public static void thenCombineExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;}, executor);CompletableFutureString completableFuture2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName());return 当前线程2执行结束;}, executor).thenCombine(completableFuture1, (msg1, msg2) - {System.out.println(completableFuture1返回结果---- msg1);System.out.println(completableFuture2返回结果---- msg2);return 所有线程执行结束;});System.out.println(主线程-------thenCombineExample-----正在执行2);System.out.println(completableFuture2.join());executor.shutdown(); // 线池需关闭 }执行结果 主线程-------thenCombineExample-----正在执行2 completableFuture1-当前线程名称completableFuture-executor-1 completableFuture2-当前线程名称completableFuture-executor-2 completableFuture1返回结果----当前线程2执行结束 completableFuture2返回结果----当前线程1执行结束 所有线程执行结束thenAcceptBoth代码示例 public static void thenAcceptBothExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;});CompletableFutureVoid completableFuture2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName());return 当前线程2执行结束;}).thenAcceptBoth(completableFuture1, (msg1, msg2) - {System.out.println(completableFuture1返回结果---- msg1);System.out.println(completableFuture2返回结果---- msg2);});System.out.println(主线程-------thenAcceptBothExample-----正在执行2);System.out.println(completableFuture2.join());executor.shutdown(); // 线池需关闭}运行结果 主线程-------thenAcceptBothExample-----正在执行2 completableFuture1-当前线程名称ForkJoinPool.commonPool-worker-9 completableFuture2-当前线程名称ForkJoinPool.commonPool-worker-2 completableFuture1返回结果----当前线程2执行结束 completableFuture2返回结果----当前线程1执行结束 nullrunAfterBoth代码示例 public static void runAfterBothExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;});CompletableFutureVoid completableFuture2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName());return 当前线程2执行结束;}).runAfterBoth(completableFuture1, () - {System.out.println(两个线程都执行结束啦----);});System.out.println(主线程-------runAfterBothExample-----正在执行2);System.out.println(completableFuture2.join());executor.shutdown(); // 线池需关闭}运行结果 主线程-------runAfterBothExample-----正在执行2 completableFuture2-当前线程名称ForkJoinPool.commonPool-worker-2 completableFuture1-当前线程名称ForkJoinPool.commonPool-worker-9 两个线程都执行结束啦---- null 2.3.2 applyToEither / acceptEither / runAfterEither ----OR组合关系 将两个CompletableFuture组合起来只要其中一个执行完了,就会执行某个任务。 区别在于 applyToEither会将已经执行完成的任务作为方法入参传递到指定方法中且有返回值acceptEither: 会将已经执行完成的任务作为方法入参传递到指定方法中且无返回值runAfterEither不会把执行结果当做方法入参且没有返回值。 applyToEither代码示例 public static void applyToEitherExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(101);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;});CompletableFutureString completableFuture2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(10001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName());return 当前线程2执行结束;}).applyToEither(completableFuture1, msg - {return msg;});System.out.println(主线程-------thenCombineExample-----正在执行2);System.out.println(completableFuture2.join());executor.shutdown(); // 线池需关闭}执行结果 主线程-------thenCombineExample-----正在执行2 completableFuture1-当前线程名称ForkJoinPool.commonPool-worker-9 当前线程1执行结束acceptEither代码示例 public static void acceptEitherExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;});CompletableFutureVoid completableFuture2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(10001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName());return 当前线程2执行结束;}).acceptEither(completableFuture1, msg - {System.out.println(获取某线程返回参数------ msg);});System.out.println(主线程-------acceptEitherExample-----正在执行2);System.out.println(completableFuture2.join());executor.shutdown(); // 线池需关闭}执行结果 主线程-------acceptEitherExample-----正在执行2 completableFuture1-当前线程名称ForkJoinPool.commonPool-worker-9 获取某线程返回参数------当前线程1执行结束 null2.3.3AllOf/AnyOf 多个任务组合处理 3AllOf:所有任务都执行完成后才执行 allOf返回的CompletableFuture。如果任意一个任务异常allOf的CompletableFuture执行get方法会抛出异常 AnyOf:任意一个任务执行完就执行anyOf返回的CompletableFuture。如果执行的任务异常anyOf的CompletableFuture执行get方法会抛出异常 AllOf代码示例 /*** 多个任务组合处理* 所有任务都执行完成后才执行 allOf返回的CompletableFuture。如果任意一个任务异常allOf的CompletableFuture执行get方法会抛出异常 *** throws ExecutionException* throws InterruptedException*/public static void allOfExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;});CompletableFutureString completableFuture2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName()); // throw new RuntimeException(咳咳执行异常啦);return 当前线程2执行结束;});CompletableFutureVoid completableFuture CompletableFuture.allOf(completableFuture1, completableFuture2).whenComplete((msg1, msg2) - {System.out.println(allOf----所有线程执行结束--- msg1);System.out.println(allOf----所有线程执行结束--- msg2);});System.out.println(主线程-------acceptEitherExample-----正在执行2);System.out.println(completableFuture.get());}执行结果 主线程-------acceptEitherExample-----正在执行2 completableFuture1-当前线程名称ForkJoinPool.commonPool-worker-9 completableFuture2-当前线程名称ForkJoinPool.commonPool-worker-2 allOf----所有线程执行结束---null allOf----所有线程执行结束---null nullAnyOf代码示例 public static void anyOfExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;});CompletableFutureString completableFuture2 CompletableFuture.supplyAsync(() - {try {Thread.sleep(1001);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(completableFuture2-当前线程名称 Thread.currentThread().getName());return 当前线程2执行结束;});CompletableFutureObject completableFuture CompletableFuture.anyOf(completableFuture1, completableFuture2).whenComplete((msg1, msg2) - {System.out.println(anyOf----所有线程执行结束--- msg1);System.out.println(anyOf----所有线程执行结束--- msg2);});System.out.println(主线程-------anyOfExample-----正在执行2);System.out.println(completableFuture.get());}运行结果 主线程-------anyOfExample-----正在执行2 completableFuture1-当前线程名称ForkJoinPool.commonPool-worker-9 completableFuture2-当前线程名称ForkJoinPool.commonPool-worker-2 anyOf----所有线程执行结束---当前线程1执行结束 anyOf----所有线程执行结束---null 当前线程1执行结束2.3.4thenCompose thenCompose方法会在某个任务执行完成后将该任务的执行结果,作为方法入参,去执行指定的方法。该方法会返回一个新的CompletableFuture实例 是对另一个CompletableFuture进行计算、操作也就是说用来连接两个CompletableFuture是生成一个新的CompletableFuture。 代码示例 public static void thenComposeExample() throws ExecutionException, InterruptedException {CompletableFutureString completableFuture1 CompletableFuture.supplyAsync(() - {System.out.println(completableFuture1-当前线程名称 Thread.currentThread().getName());return 当前线程1执行结束;},executor).thenCompose(result - CompletableFuture.supplyAsync(() - {System.out.println(result 当前线程2执行结束-当前线程名称 Thread.currentThread().getName());return result 当前线程2执行结束;},executor));System.out.println(completableFuture1.get());}执行结果 completableFuture1-当前线程名称completableFuture-executor-1 当前线程1执行结束当前线程2执行结束-当前线程名称completableFuture-executor-2 当前线程1执行结束当前线程2执行结束

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/89766.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

怎样制作一个网站网站报名照片怎么做

前言 最近因为刚入职公司开启自己的实习生涯,工作和毕设论文同步进行,导致有段时间没更新博客了,今天来分享一下最近学到的一些知识。 场景介绍 BOSS让我写一些接口,他提出这样一个需求,该接口的参数有多个&#xf…

浪漫免费表白网站宁波免费建站外包公司

在索引深入浅出:非聚集索引的B树结构在聚集表里,在聚集表里,我们看到非聚集索引的叶子层只包含非聚集索引键和聚集索引键。从聚集表结构或堆表结构里拿到剩下列,SQL Server需要进行书签/键查找操作。很多情况下书签或键查找非常消…

北京网站设计技术网站建设 自学

ClientScript.RegisterStartupScript(this.GetType(), "mb", "alert(\"提交成功\");window.location.href\"datadict.aspx\";", true); 转载于:https://www.cnblogs.com/xmyy/articles/2145635.html

做网站需要掌握什么网站营销的优缺点

如何达成目标 一、本书主要内容 推荐序一 升级你的行动工具箱 推荐序二 人们可以改变 引言 成功者和自制力的悖论 //004 自制力到底是怎样的 //007 你能做什么 //009 本书的主题 //011 1.1 准备就绪 第1章 你明白自己去往哪里吗 别说“做到最好” //017 大局与细节 //…

个体工商户在线注册手机端网站优化排名seo推广

来源:ScienceAI编辑:萝卜皮Facebook 的母公司 Meta 表示,它已经建造了一台世界上最快的研究超级计算机。Meta 研究人员 Kevin Lee 和 Shubho Sengupta 在今天的博客文章中写道,到今年年中,系统的扩展完成后&#xff0c…

域名不变 新网站各种网址大全

前端项目创建 准备工作 nodejs安装 vue cli安装 vue create frontend 最后一个y的话 它会保存 方便下次创建项目 我这是手快敲错了 随自己 前端项目组件及作用 Element-UI引入 安装 npm i element-ui -S main.js中引入 清空路口App.vue 清空Home页面 随便写个按钮 原因…

淘宝店铺网站策划wordpress网站专题

前言:常用的ORM框架有哪些 JdbcTemplate JdbcTemplate 是Spring框架提供的一个JDBC抽象库,旨在简化传统的JDBC操作,避免了繁琐的JDBC代码和数据库资源的手动处理。通过JdbcTemplate,开发者可以更加专注于业务逻辑而不是数据库的连…

做直播网站软件有哪些软件有哪些seo网站关键词优化报价

#undef 是在后面取消以前定义的宏定义 该指令的形式为 #undef 标识符 其中,标识符是一个宏名称。如果标识符当前没有被定义成一个宏名称,那么就会忽略该指令。一旦定义预处理器标识符,它将保持已定义状态且在作用域内,直到程序结束…

母婴网站建设初衷2018做网站 工具

观察者模式监听判断dom元素是否在可视区域内 本项目是使用vue3的写法。 1.IntersectionObserver IntersectionObserver可以用来自动监听元素是否进入了设备的可视区域之内,而不需要频繁的计算来做这个判断。由于可见(visible)的本质是&…

蔡家坡网站开发html网页制作步骤

给定不超过6的正整数A,考虑从A开始的连续4个数字。请输出所有由它们组成的无重复数字的3位数。 输入格式: 输入在一行中给出A。 输出格式: 输出满足条件的的3位数,要求从小到大,每行6个整数。整数间以空格分隔&#…

有没有做catalog的网站大连网站建设找哪家好

题目 幼儿园里有 N 个小朋友,老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果。 但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖果的时候&#xff…

服务器对应的网站开发语言太姥山镇建设的网站

思维导图:https://www.processon.com/view/link/5f0a6983e401fd0c8fffa75b

做网站要懂哪些杭州工程网站建设

前言大家应该都知道,整数包括负数,零,和正数。在Java中,基本类型中byte(8位)、short(16位)、int(32位)、long(64位)属于整数,并且没有无符号数,均是有符号的。对于计算机来说,它只认识二进制&am…

什么网站的地图厦门 网站优化

本节课主要是跟着教程做的,操作的东西放到作业里记录了。 这里主要记录一些视频里讲的非操作性的东西。 RAG外挂知识库?优点是成本低,不用重新训练 RAG的一个整体流程。 涉及了文本相似度匹配,是不是和传统的问答系统&#xff0…

石家庄网站建设外包保定网站推广哪家好

IoC按名称查找共分为三类: 按名称按类型按集合 按名称查找 在Spring Framework中,实时加载和延迟加载是指在容器启动时是否立即实例化bean的不同策略。下面我们将分别介绍这两种加载方式及其应用场景。 tips: 当涉及到懒加载和延时加载时&#xff0…

佛山营销网站建设制作石家庄优化seo

Maven环境搭建及配置 1.下载部署 官方网站下载正式版的Maven文件,打开bin目录,复制路径然后去环境变量中的path下配置环境变量, 如果只有一个用户只需要在上面path配置复制的路径,当然也可以直接在下面配置,下面配置默认给所有用户都配置 设置完成打开控…

dede手机医院网站模板下载辽宁省建设注册中心网站

问答系统需求文档 一、项目概述 本项目旨在开发一个能够上传 PDF 文件,并基于 PDF 内容进行问答互动的系统。用户可以上传 PDF 文件,系统将解析 PDF 内容,并允许用户通过对话框进行问答互动,获取有关 PDF 文件内容的信息。 二、…

可商用的设计网站网页设计网站总结报告怎么写

在一些应用领域,电源模块会在极端环境温度条件下工作。为了确保电源在高低温环境下可以正常运行,满足设备需求,需要对电源模块进行温度循环测试。 温度循环测试是指电源模块经过升温、保温、降温等多次循环试验来检测其在温度变化下的耐热性、…

广州网站制作方法公司做网站怎么做

如何判断exe文件是debug还是release编译生成的结论: 用IDA工具打开exe,然后看Imports里面的依赖库是否有带d或D结尾的,如果有就说明是Debug的 实验:(实验环境 vs2017, IDA工具) (0&…

怎么做网站推广临沂网站建设的基本流程包括什么

很多个人站长和中小企业在做网站的时候,会选择虚拟主机。虚拟主机用的操作系统多为Windows系统,很多人一提到操作系统立马联想到Windows系统。其实除了Windows系统外,还有很多的操作系统。其中Linux系统是其中的佼佼者。 1、操作系统 window…