首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏CSDN文章

    Completablefuture

    super T> action) … 实现Future接口是为了能够得到数据 实现CompletionStage接口是为了能够流式处理 所以CompletableFuture封装了Future使其能够方法回调避免 get()阻塞线程或者while()循环对CPU不好 Future判断任务是否完成就是get()或者idDone()循环不是很好,而Completablefuture可以直接方法回调与链式编程很方便 <Void> future1 = CompletableFuture.runAsync(() -> System.out.println("runAsync"), Executors.newFixedThreadPool (4)); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "supplyAsync", Executors.newFixedThreadPool (4)); CompletableFuture.anyOf(future1, future2); CompletableFuture.allOf(future1, future2); future2.thenApply

    24010编辑于 2024-04-11
  • 来自专栏OSChina

    CompletableFuture

    47120编辑于 2022-07-09
  • 来自专栏快乐阿超

    CompletableFuture

    public void run() { LineUtils.print("原始异步Runnable"); } }); 现在咱们使用1.8的CompletableFuture <Integer> async = CompletableFuture.supplyAsync(() -> { System.out.println("1.8新版CompletableFuture 调用Callable"); return 1; }); final CompletableFuture<Void> future = CompletableFuture.runAsync (CompletableFutureDemo::runnable, executor); CompletableFuture<Integer> future = CompletableFuture (future, future, future).get(); // 等待所有结果完成 CompletableFuture.allOf(future, future, future

    48220编辑于 2022-08-16
  • 来自专栏后端技术学习

    CompletableFuture学习

    因此,我们有必要了解CompletableFuture,同时其也是真正意义上的异步编程的实现。 package com.study.concurrent.completableFuture; import java.util.Random; import java.util.concurrent.CompletableFuture <String> future1 = CompletableFuture.supplyAsync(()-> "hello"); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()-> "hello"); CompletableFuture<String> result = future1.thenCombine ; } });*/ CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync((

    1.2K20发布于 2020-07-16
  • 来自专栏房东的猫

    CompletableFuture介绍

    public class CompletableFuture<T> implements Future<T>, CompletionStage<T> 既然CompletableFuture类实现了CompletionStage CompletableFuture提供了方法,能够显式地完成这个future,所以它叫CompletableFuture。 public static CompletableFuture<Void> runAsync(Runnable runnable) public static CompletableFuture<Void <String> completableFuture = CompletableFuture.supplyAsync(() -> { printTimeAndThread("厨师炒菜 <String> completableFuture = CompletableFuture.supplyAsync(() -> { printTimeAndThread("厨师炒菜

    2.3K81发布于 2021-07-12
  • 来自专栏Eliauk的小窝

    CompletableFuture详解

    CompletableFuture详解 回顾Future 因为CompletableFuture实现了Future接口所以先看一下Future Future是Java5新加的一个接口,它提供了一种异步并行计算的功能 实际上,它CompletableFuture使用了默认线程池是ForkJoinPool.commonPool。 CompletableFuture提供了几十种方法,辅助我们的异步任务场景。 CompletableFuture 创建异步任务 CompletableFuture创建异步任务,一般有supplyAsync和runAsync两个方法 supplyAsync执行CompletableFuture 任务,支持返回值 runAsync执行CompletableFuture任务,没有返回值。 thenRunAsync public CompletableFuture<Void> thenRun(Runnable action); public CompletableFuture<Void>

    1.3K20编辑于 2022-11-15
  • 来自专栏菩提树下的杨过

    CompletableFuture笔记

    CompletableFuture是java8引入的一个很实用的特性,可以视为Future的升级版本,以下几个示例可以说明其主要用法(注:示例来自《java8实战》一书第11章) 一、引子:化同步为异步 需要说明的是:CompletableFuture内部其实也是使用线程池来处理的,只不过这个线程池的类型默认是ForkJoinPool,这一点可以从java.util.concurrent.CompletableFuture ForkJoinPool.commonPool() : new ThreadPerTaskExecutor(); 三、CompletableFuture中使用自定义线程池 如果需要查询报价的商家有很多, 如果换成用CompletableFuture默认的ForkJoinPool呢,性能会不会好一些? [size]); CompletableFuture.allOf(futureArray).join(); } 解释:主要是利用了CompletableFuture.allOf(

    83510发布于 2021-06-10
  • 来自专栏TopCoder

    CompletableFuture 应用实践

    CompletableFuture 类实现了CompletionStage和Future接口,所以还可以像之前使用Future那样使用CompletableFuture ,尽管已不再推荐这样用了。 接下来就一起看下CompletableFuture 类的使用吧~ CompletableFuture的创建 CompletableFuture 类的创建可以自己设置future的result,示例如下: // 创建一个带result的CompletableFuture CompletableFuture<String> future = CompletableFuture.completedFuture public static CompletableFuture<Void> allOf(CompletableFuture<? >... cfs) public static CompletableFuture<Object> anyOf(CompletableFuture<?

    84610发布于 2020-12-02
  • 来自专栏明明如月的技术专栏

    CompletableFuture 使用介绍

    中是怎么写的,看下面的几行代码: CompletableFuture cfA = CompletableFuture.supplyAsync(() -> "resultA"); CompletableFuture public static CompletableFuture allOf(CompletableFuture... cfs){...} public static CompletableFuture CompletableFuture cfA = CompletableFuture.supplyAsync(() -> "resultA"); CompletableFuture cfB = CompletableFuture.supplyAsync (() -> 123); CompletableFuture cfC = CompletableFuture.supplyAsync(() -> "resultC"); CompletableFuture (() -> "resultA"); CompletableFuture cfB = CompletableFuture.supplyAsync(() -> 123); CompletableFuture

    1.1K31发布于 2021-08-31
  • 来自专栏开发工具/IDEA

    CompletableFutureCompletableFuture中join()和get()方法的区别

    CompletableFutureCompletableFuture中join()和get()方法的区别相同点:  join()和get()方法都是阻塞调用它们的线程(通常为主线程)来获取CompletableFuture 这里再强调一下:CompletableFuture.get() 和 CompletableFuture.join() 这两个方法是获取异步守护线程的返回值的。 ps: stage就是 CompletionStage 也就是 CompletableFuture 实现的接口,意思就是每一个 CompletableFuture的任务返回都是一个stage看代码:public public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFuture <Integer> future = CompletableFuture.supplyAsync(() -> multipart(5)); // System.out.println(future.join

    2.9K00编辑于 2023-03-25
  • 来自专栏犀牛饲养员的技术笔记

    详解 java CompletableFuture

    另外通过这个示例,可以发现我们完全可以使用 CompletableFuture 代替 Future。 当然 CompletableFuture 的功能远不止与此,不然它的存在就没有意义了。 比如下面这个例子: CompletableFuture<PersonInfo> personInfoCompletableFuture = CompletableFuture.supplyAsync(( = CompletableFuture.supplyAsync(() -> addressService.getAddress(personId)); final CompletableFuture 下面来看个示例, CompletableFuture<PersonInfo> personInfoCompletableFuture = CompletableFuture.supplyAsync(() = CompletableFuture.supplyAsync(() -> addressService.getAddress(personId)); final CompletableFuture

    84930发布于 2020-08-10
  • 来自专栏强仔仔

    CompletableFuture基本用法

    -> System.out.print(x)).thenRun(() -> System.out.println()) 一个阶段的执行可能是被单个阶段的完成触发,也可能是由多个阶段一起触发 CompletableFuture 在Java8中,CompletableFuture提供了非常强大的Future的扩展功能,可以帮助我们简化异步编程的复杂性,并且提供了函数式编程的能力,可以通过回调的方式处理计算结果,也提供了转换和组合 CompletableFuture 的方法。 CompletableFuture基本用法 创建CompletableFuture ? thenApply ? 当前阶段正常完成以后执行,而且当前阶段的执行的结果会作为下一阶段的输入参数。 事实证明,只有当每个操作很复杂需要花费相对很长的时间(比如,调用多个其它的系统的接口;比如,商品详情页面这种需要从多个系统中查数据显示的)的时候用CompletableFuture才合适,不然区别真的不大

    81710发布于 2019-05-25
  • 来自专栏java 成神之路

    CompletableFuture 使用详解

    1、 runAsync 和 supplyAsync方法 CompletableFuture 提供了四个静态方法来创建一个异步操作。 public static CompletableFuture<Void> runAsync(Runnable runnable) public static CompletableFuture<Void 示例 //无返回值 public static void runAsync() throws Exception { CompletableFuture<Void> future = CompletableFuture.runAsync 示例 public static void whenComplete() throws Exception { CompletableFuture<Void> future = CompletableFuture.runAsync public CompletableFuture thenApply(Function<? super T,?

    4.2K41发布于 2018-10-10
  • 来自专栏全栈开发那些事

    CompletableFuture异步编排

    CompletableFuture异步编排 1、CompletableFuture异步编排 1.1 为什么需要异步编排   问题:查询商品详情页的逻辑非常复杂,数据的获取都需要远程调用,必然需要花费更多的时间 ,并且提供了转换和组合CompletableFuture的方法。 > completableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() { @Override 因为如果是串行化的化,那么即使B休眠一会,那么C也会一直等着,输出顺序为B、C 1.5 多任务组合 public static CompletableFuture<Void> allOf(CompletableFuture >... cfs); public static CompletableFuture<Object> anyOf(CompletableFuture<?

    1K20编辑于 2023-04-23
  • 来自专栏啸天"s blog

    Java CompletableFuture 详解

    创建CompletableFuture对象。 CompletableFuture.completedFuture是一个静态辅助方法,用来返回一个已经计算好的CompletableFuture。 ,当原始的CompletableFuture抛出异常的时候,就会触发这个CompletableFuture的计算,调用function计算值,否则如果原始的CompletableFuture正常计算完后 因此它的功能相当于将CompletableFuture<T>转换成CompletableFuture。 的计算值,返回结果将是一个新的CompletableFuture,这个新的CompletableFuture会组合原来的CompletableFuture和函数返回的CompletableFuture。 比如有这样一个需求,将多个CompletableFuture组合成一个CompletableFuture,这个组合后的CompletableFuture的计算结果是个List,它包含前面所有的CompletableFuture

    1.8K10发布于 2021-04-19
  • 来自专栏Java识堂

    异步神器CompletableFuture

    Java8新增的CompletableFuture则借鉴了Netty等对Future的改造,简化了异步编程的复杂性,并且提供了函数式编程的能力 创建CompletableFuture对象 方法名 描述 ()); CompletableFuture<Void> voidFuture = CompletableFuture.runAsync(() -> System.out.println("hello ); CompletableFuture future2 = CompletableFuture.supplyAsync(() -> { sleepRandom(); return "Java 完成后执行计算 anyOf 任意一个CompletableFuture完成后执行计算 allOf的使用 CompletableFuture<String> future1 = CompletableFuture.supplyAsync CompletableFuture<Object> resultFuture = CompletableFuture.anyOf(future1, future2, future3); // 欢迎关注

    1.4K20发布于 2020-09-14
  • 来自专栏JAVA

    CompletableFuture 异步处理

    CompletableFuture.supplyAsync()也可以用来创建CompletableFuture实例。 简单来说也就是第一种是使用默认线程池的,第二种则可以指定线程池;有返回值的异步任务; CompletableFuture.runAsync()也可以用来创建CompletableFuture实例。 :CompletableFuture是多个任务都执行完成后才会执行,只有有一个任务执行异常,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回null。 anyOf :CompletableFuture是多个任务只要有一个任务执行完成,则返回的CompletableFuture执行get方法时会抛出异常,如果都是正常执行,则get返回执行完成任务的结果 CompletableFuture<Integer> callFuture2 = CompletableFuture.supplyAsync(()->call2(3,4)); //异步方法

    34910编辑于 2024-11-20
  • 来自专栏JavaEE

    如何使用CompletableFuture

    CompletableFuture API CompletableFuture 类中提供了很多API,常见的有以下几个: 创建CompletableFuture 我们可以通过以下两种方式创建CompletableFutureCompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World"); CompletableFuture CompletableFuture用法示例 创建CompletableFuture CompletableFuture<String> future = CompletableFuture.supplyAsync thenRun 示例 CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello World"); CompletableFuture thenCombine 示例 CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture

    75230编辑于 2023-10-16
  • 来自专栏java学习java

    CompletableFuture 异步编排

    创建异步对象  CompletableFuture 提供了四个静态方法来创建一个异步操作。 CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> { System.out.println CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> { System.out.println(" CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> { System.out.println(" CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> { System.out.println("

    41250编辑于 2023-10-15
  • 来自专栏Java随想录

    CompletableFuture深度解析

    执行流程 CompletableFuture 的执行流程如下: 创建CompletableFuture对象:通过调用CompletableFuture类的构造方法或静态工厂方法创建一个新的CompletableFuture 以下是这些方法的介绍: 创建对象 创建一个 CompletableFuture 对象有以下几种方法: 使用 CompletableFuture 的构造方法 CompletableFuture<String > future = new CompletableFuture<>(); 使用 CompletableFuture 的静态工厂方法 CompletableFuture<String> future //CompletableFuture.completedFuture()直接创建一个已完成状态的CompletableFuture CompletableFuture<String> cf2 = CompletableFuture.completedFuture CompletableFuture对象作为参数,并返回一个新的CompletableFuture对象,该对象在所有给定的CompletableFuture都完成时完成。

    87010编辑于 2024-01-11
领券