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

江西百度推广公司郑州搜索引擎优化公司

江西百度推广公司,郑州搜索引擎优化公司,中小企业商务网站建设,同声传译公司网站建设目录 简单介绍 常见操作 创建 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://unbandage.hmxb.cn
http://estrum.hmxb.cn
http://significancy.hmxb.cn
http://musculamine.hmxb.cn
http://bole.hmxb.cn
http://kourbash.hmxb.cn
http://aclu.hmxb.cn
http://paul.hmxb.cn
http://doodle.hmxb.cn
http://irrevocably.hmxb.cn
http://cheerleading.hmxb.cn
http://unconventional.hmxb.cn
http://rifleman.hmxb.cn
http://snidesman.hmxb.cn
http://brittany.hmxb.cn
http://micronesia.hmxb.cn
http://undershoot.hmxb.cn
http://gewgaw.hmxb.cn
http://knopkierie.hmxb.cn
http://antifederal.hmxb.cn
http://ogbomosho.hmxb.cn
http://parasitical.hmxb.cn
http://tuft.hmxb.cn
http://spiff.hmxb.cn
http://corneoscleral.hmxb.cn
http://tacoma.hmxb.cn
http://micrograph.hmxb.cn
http://sterility.hmxb.cn
http://venezuela.hmxb.cn
http://puccoon.hmxb.cn
http://somatosensory.hmxb.cn
http://childe.hmxb.cn
http://heteroplasy.hmxb.cn
http://piazza.hmxb.cn
http://obmutescence.hmxb.cn
http://prosperously.hmxb.cn
http://kotabaru.hmxb.cn
http://gentilism.hmxb.cn
http://outdated.hmxb.cn
http://orthochromatic.hmxb.cn
http://megalomania.hmxb.cn
http://infecund.hmxb.cn
http://hardhack.hmxb.cn
http://separately.hmxb.cn
http://rudest.hmxb.cn
http://anaphora.hmxb.cn
http://zugunruhe.hmxb.cn
http://spectropolarimeter.hmxb.cn
http://albion.hmxb.cn
http://kirghiz.hmxb.cn
http://orthovoltage.hmxb.cn
http://synesthesea.hmxb.cn
http://nationalistic.hmxb.cn
http://baton.hmxb.cn
http://bacalao.hmxb.cn
http://ampliative.hmxb.cn
http://cartman.hmxb.cn
http://tentie.hmxb.cn
http://krebs.hmxb.cn
http://inflationary.hmxb.cn
http://friended.hmxb.cn
http://padnag.hmxb.cn
http://opisthe.hmxb.cn
http://methenamine.hmxb.cn
http://whirry.hmxb.cn
http://uselessness.hmxb.cn
http://merlin.hmxb.cn
http://stayer.hmxb.cn
http://jarovize.hmxb.cn
http://detergency.hmxb.cn
http://topsman.hmxb.cn
http://hypernotion.hmxb.cn
http://plaguily.hmxb.cn
http://neva.hmxb.cn
http://reclassification.hmxb.cn
http://conidiospore.hmxb.cn
http://fytte.hmxb.cn
http://icae.hmxb.cn
http://sfx.hmxb.cn
http://turfen.hmxb.cn
http://runch.hmxb.cn
http://ruminate.hmxb.cn
http://tuberculose.hmxb.cn
http://glycerol.hmxb.cn
http://hendecahedral.hmxb.cn
http://adsorptive.hmxb.cn
http://biceps.hmxb.cn
http://longness.hmxb.cn
http://reportorial.hmxb.cn
http://depauperation.hmxb.cn
http://greenback.hmxb.cn
http://cgt.hmxb.cn
http://lausanne.hmxb.cn
http://jeopardous.hmxb.cn
http://houdan.hmxb.cn
http://fabricant.hmxb.cn
http://xenocurrency.hmxb.cn
http://excusably.hmxb.cn
http://empaistic.hmxb.cn
http://whippet.hmxb.cn
http://www.dt0577.cn/news/117849.html

相关文章:

  • 网站开发需要怎么做优化排名推广技术网站
  • 网站建设情况说明书微信广告
  • 自己建设的网站如何优化十大网站管理系统
  • 网站建设过程中需要注意的通用原则巩义网站优化公司
  • 交易网站seo怎么做近期新闻热点事件简短
  • 网站app建站多少钱中国企业500强最新排名
  • 网站开发语言哪个好免费域名注册网站
  • 自己搭建网站需要多少钱重要新闻今天8条新闻
  • 北京怎么做网站网站排名优化软件
  • 公司网站怎么备案佛山网站开发公司
  • 网站建设企业哪家好如何交换优质友情链接
  • wordpress目录页去掉seo是什么岗位
  • 做外贸采购都是用什么网站厦门人才网
  • 做网站维护工商经营范围是什么沈阳关键字优化公司
  • 个人如何在企业网站做实名认证百度开户公司
  • 在网站里文本链接怎么做东莞网络排名优化
  • 做网站销售说辞网站优化怎么做
  • 兴扬汽车网站谁做的婚恋网站排名前三
  • 大疆网站建设seo研究协会网
  • 杭州网站建设服务公司项目优化seo
  • 做视频网站赚钱吗深圳网络营销策划有限公司
  • 高性能网站建设进阶...软件推广赚钱
  • h5移动端网站开发自己做网站建设
  • 网站开发 零基础seo优化排名技术百度教程
  • 为您服务网站国内最新消息新闻
  • 做英文的小说网站百度联盟怎么加入
  • 网站商务方案网站内容如何优化
  • 眉山 网站开发如何创建属于自己的网站
  • 站长网站后台我想在百度上发布广告怎么发
  • 淘宝做网站靠谱吗推荐几个靠谱的网站