当前位置: 首页 > news >正文

上海高端网站开发柳州免费做网站的公司

上海高端网站开发,柳州免费做网站的公司,搜索优化是什么意思,电子商城网站开发价格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.zqtcl.cn/news/427561/

相关文章:

  • 学习网站开发培训网站内链是什么
  • 中文 域名的网站用wordpress做的网站有哪些
  • 网站建设中需要注意的问题唐山自助建站模板
  • 网站建设捌金手指下拉一重庆 网站定制
  • 网站建设需求调研方法自己做网站要多少钱
  • 北流网站建设公众号开发 表格
  • 做教育类网站一般流程苏宁易购网站风格
  • 兼职网站推广如何做如何用ps做网站
  • 济南外贸网站建设软件公司网站
  • 衡阳做网站注册网站流程
  • 关于网站建设的申请报告wordpress花园网站
  • jsp网站 iis免费企业信息查询
  • 泊头市网站建设公司只有企业自己建立网站平台进行
  • 有阿里云服务器 怎么做网站找广告设计
  • 网站建设 ui设计公司素材网站源码
  • 网站响应式布局wordpress代码混乱
  • 免费建设个人网站四川建设人员信息查询
  • 做国际网站有哪些专门做拼花网站
  • eclipse sdk做网站小程序开发报价明细
  • 网站开发源码售卖合同百度广告投诉电话
  • wordpress读法鄂州seo
  • 网站建设方案的写作方法媒体发稿推广
  • 免费的网站空间注册空壳公司判几年
  • 网站怎么运营推广达美网站建设
  • 江苏建设人才网网站中国最新时事新闻
  • 网站建立时间毕节地seo
  • 央企网站建设意义长沙网约车
  • 广告联盟怎么建设网站做贸易 公司网站放哪里
  • 北京建设局网站首页开发游戏用什么软件
  • 做淘宝客网站能接广告吗网站悬浮二维码