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

小企业网站建设有什么用代做网站微信号

小企业网站建设有什么用,代做网站微信号,广州seo黑帽培训,邢台优化网站排名目录 简单介绍 常见操作 创建 CompletableFuture new 关键字 静态工厂方法 处理异步结算的结果 简单介绍 CompletableFuture 同时实现了 Future 和 CompletionStage 接口。 public class CompletableFutureT implements FutureT, CompletionStageT…目录 简单介绍 常见操作 创建 CompletableFuture new 关键字 静态工厂方法 处理异步结算的结果 简单介绍 CompletableFuture 同时实现了 Future 和 CompletionStage 接口。 public class CompletableFutureT implements FutureT, CompletionStageT { }CompletableFuture 除了提供了更为好用和强大的 Future 特性之外还提供了函数式编程的能力。 Future 接口有 5 个方法 boolean cancel(boolean mayInterruptIfRunning)尝试取消执行任务。boolean isCancelled()判断任务是否被取消。boolean isDone()判断任务是否已经被执行完成。get()等待任务执行完成并获取运算结果。get(long timeout, TimeUnit unit)多了一个超时时间。 CompletionStage 接口描述了一个异步计算的阶段。很多计算可以分成多个阶段或步骤此时可以通过它将所有步骤组合起来形成异步计算的流水线。 CompletionStage 接口中的方法比较多CompletableFuture 的函数式能力就是这个接口赋予的。从这个接口的方法参数你就可以发现其大量使用了 Java8 引入的函数式编程。 常见操作 创建 CompletableFuture 常见的创建 CompletableFuture 对象的方法如下 通过 new 关键字。基于 CompletableFuture 自带的静态工厂方法runAsync()、supplyAsync() 。 new 关键字 通过 new 关键字创建 CompletableFuture 对象这种使用方式可以看作是将 CompletableFuture 当做 Future 来使用。 下面咱们来看一个简单的案例。 我们通过创建了一个结果值类型为 RpcResponseObject 的 CompletableFuture你可以把 resultFuture 看作是异步运算结果的载体。 CompletableFutureRpcResponseObject resultFuture new CompletableFuture();假设在未来的某个时刻我们得到了最终的结果。这时我们可以调用 complete() 方法为其传入结果这表示 resultFuture 已经被完成了。 // complete() 方法只能调用一次后续调用将被忽略。 resultFuture.complete(rpcResponse);你可以通过 isDone() 方法来检查是否已经完成。 public boolean isDone() {return result ! null; }获取异步计算的结果也非常简单直接调用 get() 方法即可。调用 get() 方法的线程会阻塞直到 CompletableFuture 完成运算。 rpcResponse completableFuture.get();如果你已经知道计算的结果的话可以使用静态方法 completedFuture() 来创建 CompletableFuture 。 CompletableFutureString future CompletableFuture.completedFuture(hello!); assertEquals(hello!, future.get());completedFuture() 方法底层调用的是带参数的 new 方法只不过这个方法不对外暴露。 public static U CompletableFutureU completedFuture(U value) {return new CompletableFutureU((value null) ? NIL : value); }静态工厂方法 这两个方法可以帮助我们封装计算逻辑。 static U CompletableFutureU supplyAsync(SupplierU supplier); // 使用自定义线程池(推荐) static U CompletableFutureU supplyAsync(SupplierU supplier, Executor executor); static CompletableFutureVoid runAsync(Runnable runnable); // 使用自定义线程池(推荐) static CompletableFutureVoid runAsync(Runnable runnable, Executor executor);runAsync() 方法接受的参数是 Runnable 这是一个函数式接口不允许返回值。当你需要异步操作且不关心返回结果的时候可以使用 runAsync() 方法。 FunctionalInterface public interface Runnable {public abstract void run(); }supplyAsync() 方法接受的参数是 SupplierU 这也是一个函数式接口U 是返回结果值的类型。 FunctionalInterface public interface SupplierT {/*** Gets a result.** return a result*/T get(); }当你需要异步操作且关心返回结果的时候,可以使用 supplyAsync() 方法。 CompletableFutureVoid future CompletableFuture.runAsync(() - System.out.println(hello!)); future.get();// 输出 hello! CompletableFutureString future2 CompletableFuture.supplyAsync(() - hello!); assertEquals(hello!, future2.get());处理异步结算的结果 当我们获取到异步计算的结果之后还可以对其进行进一步的处理比较常用的方法有下面几个 thenApply()thenAccept()thenRun()whenComplete() thenApply() 方法接受一个 Function 实例用它来处理结果。 // 沿用上一个任务的线程池 public U CompletableFutureU thenApply(Function? super T,? extends U fn) {return uniApplyStage(null, fn); }//使用默认的 ForkJoinPool 线程池不推荐 public U CompletableFutureU thenApplyAsync(Function? super T,? extends U fn) {return uniApplyStage(defaultExecutor(), fn); } // 使用自定义线程池(推荐) public U CompletableFutureU thenApplyAsync(Function? super T,? extends U fn, Executor executor) {return uniApplyStage(screenExecutor(executor), fn); }如果你不需要从回调函数中获取返回结果可以使用 thenAccept() 或者 thenRun()。这两个方法的区别在于 thenRun() 不能访问异步计算的结果。 thenAccept() 方法的参数是 Consumer? super T 。 public CompletableFutureVoid thenAccept(Consumer? super T action) {return uniAcceptStage(null, action); }public CompletableFutureVoid thenAcceptAsync(Consumer? super T action) {return uniAcceptStage(defaultExecutor(), action); }public CompletableFutureVoid thenAcceptAsync(Consumer? super T action,Executor executor) {return uniAcceptStage(screenExecutor(executor), action); }
http://www.dnsts.com.cn/news/66232.html

相关文章:

  • 给个做的网站吗wordpress怎么做seo关键词
  • 专业做汽配的网站东莞网站建设那家好
  • 佛山个人制作网站公司wordpress采集查卷
  • 网站图片太多怎么办公司网站毕业设计论文
  • 库尔勒网站建设推广建造网站
  • 专门做顶账房的网站做网站域名优化的怎么样
  • 邯郸营销型网站建设公司网站建设调研问卷
  • 浙江公铁建设工程有限公司网站最有效的招商方式
  • 网站模板 站长之家套版网站怎么做
  • 吉安市建设技术培训中心网站c 网站开发类似优酷
  • 房车网站建设意义大芬地铁站附近做网站
  • 汶上网站建设多少钱山东网站建设平台
  • 优化官方网站设计靖江 建设局网站
  • app和手机网站工程建设项目招标范围和规模标准规定
  • 架设网站需要什么欧阳网站建设
  • python php 做网站在线图片编辑器手机版
  • 哪里做网站的比较多wordpress关键词描述
  • 网上网站建设教程李贤威wordpress建站教程
  • 建设银官方网站白帽seo是什么职业
  • 建阳建设局网站网站建设学多长时间
  • 网站建设阿胶膏的作用网站利用百度离线地图
  • 做网站是如果盈利的湖北省建设工程网站
  • iis网站没有属性宁波建设工程信息网
  • 自己怎样建网站如何提高自己的营销能力
  • 建站培训班做兼职去什么网站
  • 安卓开发软件seo都用在哪些网站
  • 网站建设程序员做什么网站设计费报价表
  • 网站怎么做优化做一个展示型网站多少钱
  • 较好网站建设公司做移动网站优化排名首页
  • vR网站建设程序建网页的软件