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

廊坊网站建设费用营销型网站建设易网拓

廊坊网站建设费用,营销型网站建设易网拓,怎样建设网站首页,南宁外包seo服务目录 简单介绍 常见操作 创建 CompletableFuture new 关键字 静态工厂方法 处理异步结算的结果 简单介绍 CompletableFuture 同时实现了 Future 和 CompletionStage 接口。 public class CompletableFuture<T> implements Future<T>, CompletionStage<T…

目录

简单介绍

常见操作

创建 CompletableFuture

new 关键字

静态工厂方法

处理异步结算的结果


简单介绍

CompletableFuture 同时实现了 Future 和 CompletionStage 接口。

public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
}

 CompletableFuture 除了提供了更为好用和强大的 Future 特性之外,还提供了函数式编程的能力。

Future 接口有 5 个方法:

  • boolean cancel(boolean mayInterruptIfRunning):尝试取消执行任务。
  • boolean isCancelled():判断任务是否被取消。
  • boolean isDone():判断任务是否已经被执行完成。
  • get():等待任务执行完成并获取运算结果。
  • get(long timeout, TimeUnit unit):多了一个超时时间。

CompletionStage 接口描述了一个异步计算的阶段。很多计算可以分成多个阶段或步骤,此时可以通过它将所有步骤组合起来,形成异步计算的流水线。

CompletionStage 接口中的方法比较多,CompletableFuture 的函数式能力就是这个接口赋予的。从这个接口的方法参数你就可以发现其大量使用了 Java8 引入的函数式编程。

常见操作

 

创建 CompletableFuture

常见的创建 CompletableFuture 对象的方法如下:

  1. 通过 new 关键字。
  2. 基于 CompletableFuture 自带的静态工厂方法:runAsync()supplyAsync()

new 关键字

通过 new 关键字创建 CompletableFuture 对象这种使用方式可以看作是将 CompletableFuture 当做 Future 来使用。

下面咱们来看一个简单的案例。

我们通过创建了一个结果值类型为 RpcResponse<Object>CompletableFuture,你可以把 resultFuture 看作是异步运算结果的载体。

CompletableFuture<RpcResponse<Object>> resultFuture = new CompletableFuture<>();

假设在未来的某个时刻,我们得到了最终的结果。这时,我们可以调用 complete() 方法为其传入结果,这表示 resultFuture 已经被完成了。

// complete() 方法只能调用一次,后续调用将被忽略。
resultFuture.complete(rpcResponse);

你可以通过 isDone() 方法来检查是否已经完成。

public boolean isDone() {return result != null;
}

获取异步计算的结果也非常简单,直接调用 get() 方法即可。调用 get() 方法的线程会阻塞直到 CompletableFuture 完成运算。

rpcResponse = completableFuture.get();

如果你已经知道计算的结果的话,可以使用静态方法 completedFuture() 来创建 CompletableFuture 。

CompletableFuture<String> future = CompletableFuture.completedFuture("hello!");
assertEquals("hello!", future.get());

completedFuture() 方法底层调用的是带参数的 new 方法,只不过,这个方法不对外暴露。

public static <U> CompletableFuture<U> completedFuture(U value) {return new CompletableFuture<U>((value == null) ? NIL : value);
}

静态工厂方法

这两个方法可以帮助我们封装计算逻辑。

static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier);
// 使用自定义线程池(推荐)
static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor);
static CompletableFuture<Void> runAsync(Runnable runnable);
// 使用自定义线程池(推荐)
static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor);

runAsync() 方法接受的参数是 Runnable ,这是一个函数式接口,不允许返回值。当你需要异步操作且不关心返回结果的时候可以使用 runAsync() 方法。

@FunctionalInterface
public interface Runnable {public abstract void run();
}

supplyAsync() 方法接受的参数是 Supplier<U> ,这也是一个函数式接口,U 是返回结果值的类型。

@FunctionalInterface
public interface Supplier<T> {/*** Gets a result.** @return a result*/T get();
}

当你需要异步操作且关心返回结果的时候,可以使用 supplyAsync() 方法。

CompletableFuture<Void> future = CompletableFuture.runAsync(() -> System.out.println("hello!"));
future.get();// 输出 "hello!"
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "hello!");
assertEquals("hello!", future2.get());

处理异步结算的结果

当我们获取到异步计算的结果之后,还可以对其进行进一步的处理,比较常用的方法有下面几个:

  • thenApply()
  • thenAccept()
  • thenRun()
  • whenComplete()

thenApply() 方法接受一个 Function 实例,用它来处理结果。

// 沿用上一个任务的线程池
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn) {return uniApplyStage(null, fn);
}//使用默认的 ForkJoinPool 线程池(不推荐)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn) {return uniApplyStage(defaultExecutor(), fn);
}
// 使用自定义线程池(推荐)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor) {return uniApplyStage(screenExecutor(executor), fn);
}

如果你不需要从回调函数中获取返回结果,可以使用 thenAccept() 或者 thenRun()。这两个方法的区别在于 thenRun() 不能访问异步计算的结果。

thenAccept() 方法的参数是 Consumer<? super T> 。

public CompletableFuture<Void> thenAccept(Consumer<? super T> action) {return uniAcceptStage(null, action);
}public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) {return uniAcceptStage(defaultExecutor(), action);
}public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor) {return uniAcceptStage(screenExecutor(executor), action);
}

http://www.shuangfujiaoyu.com/news/38108.html

相关文章:

  • 网站百度权重怎么提升郑州seo哪家专业
  • zencart网站地图生成郑州网络推广哪家口碑好
  • 做网站主页新网站如何快速收录
  • 云南网站开发哪家靠谱免费发广告网站
  • 美国地接公司怎么在中国网站做推广湘潭网站设计外包公司
  • 建设网站需要先构建好模型三门峡网站seo
  • 湖南新能源公司中企动力网站建设网络顾问
  • 设计师分六个级别seo排名平台
  • 只做域名跳转和关停网站今日新闻国际头条新闻
  • 网站优化软件开发软文营销文章300字
  • 做测试题的网站福州网站开发公司
  • 网站仿造产品软文代写
  • 做ppt好的网站青岛网站设计公司哪家好
  • 学做家常菜的网站 知乎营销策略分析包括哪些内容
  • 企业网站排名提升软件能优化今日国内新闻最新消息大事
  • 网站设计要求有哪些做网站的软件叫什么
  • 网站建设策划目的及过程竞价推广教程
  • 哈尔滨网站建设排行网页制作工具有哪些
  • 哪里有好的网站策划网络营销活动
  • 仓储网站模板精准客户资源购买
  • 广州网站开发解决方案矿产网站建设价格
  • 怎么做一个电商网站seo优化自动点击软件
  • 网站建设上传服务器步骤磁力搜索引擎
  • 石家庄网站定制制作口碑营销的形式
  • 成全视频免费观看在线看主演惠州seo关键字排名
  • 公司做网站需要什么资质免费的域名和网站
  • 做动画的网站有哪些百度搜索引擎营销如何实现
  • idc销售网站源码推广系统
  • 网站如何提升seo排名潍坊网站关键词推广
  • 如何给网站流量来源做标记通过在网址后边加问号?全网搜索指数