介绍

JDK1.8引入CompletableFuture类。

使用方法


public class CompletableFutureTest { private static ExecutorService threadPool = new ThreadPoolExecutor(40, 100,
0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(20)); public String B() {
System.out.println("执行方法B");
sleep(5);
return "Function B";
} public String C() {
System.out.println("执行方法C");
sleep(20);
return "Function C";
} public void sleep(int i) {
try {
Thread.sleep(i * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} public void testCompableFuture() {
CompletableFuture<String> future;
try {
//Returns a new CompletableFuture
// that is asynchronously completed by a task running in the given executor
// with the value obtained by calling the given Supplier.
future = CompletableFuture.supplyAsync(() -> B(), threadPool);
//若去掉线程池,有何区别future = CompletableFuture.supplyAsync(() -> B()); sleep(9);
System.out.println(future.toString());
System.out.println(future.isDone());
} catch (RejectedExecutionException e) {
System.out.println("调用搜索列表服务线程满负荷, param:{}");
}
} public static void main(String[] args) {
CompletableFutureTest test = new CompletableFutureTest();
test.testCompableFuture();
} }

API

supplyAsync方法

JDK方法描述

/**
* Returns a new CompletableFuture that is asynchronously completed
* by a task running in the given executor with the value obtained
* by calling the given Supplier.
*
* @param supplier a function returning the value to be used
* to complete the returned CompletableFuture
* @param executor the executor to use for asynchronous execution
* @param <U> the function's return type
* @return the new CompletableFuture
*/
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
Executor executor) {
return asyncSupplyStage(screenExecutor(executor), supplier);
}

应用场景

请求A的执行方法X,需满足下列需求:

①请求B、C、D中任一一个请求有返回结果,则X方法返回响应结果。

②请求B、C、D中都执行完,则X方法返回响应结果。

源码阅读

依赖关系

参考文档

JDK API文档

20 个使用 Java CompletableFuture的例子

Java核心复习——CompletableFuture的更多相关文章

  1. Java核心复习——J.U.C AbstractQueuedSynchronizer

    第一眼看到AbstractQueuedSynchronizer,通常都会有这几个问题. AbstractQueuedSynchronizer为什么要搞这么一个类? 这个类是干什么的.有什么用? 这个类 ...

  2. Java核心复习——线程池ThreadPoolExecutor源码分析

    一.线程池的介绍 线程池一种性能优化的重要手段.优化点在于创建线程和销毁线程会带来资源和时间上的消耗,而且线程池可以对线程进行管理,则可以减少这种损耗. 使用线程池的好处如下: 降低资源的消耗 提高响 ...

  3. Java核心复习—— 原子性、有序性与Happens-Before

    一. 产生并发Bug的源头 可见性 缓存导致的可见性问题 原子性 线程切换带来的原子性问题 有序性 编译优化带来的有序性问题 上面讲到了 volatile 与可见性,本章再主要讲下原子性.有序性与Ha ...

  4. Java核心复习 —— J.U.C 并发工具类

    一.CountDownLatch 文档描述 A synchronization aid that allows one or more threads to wait until* a set of ...

  5. Java核心复习——synchronized

    一.概念 利用锁机制实现线程同步,synchronized关键字的底层交由了JVM通过C++来实现 Java中的锁有两大特性: 互斥性 同一时间,只允许一个线程持有某个对象锁. 可见性 锁释放前,线程 ...

  6. Java核心复习—— volatile 与可见性

    一.介绍 volatile保证共享变量的"可见性".可见性指的是当一个线程修改变量时,另一个线程能读到这个修改的值. 这里就要提出几个问题. 问题1:为什么一个线程修改时,另一个线 ...

  7. Java核心复习—— ThreadLocal源码分析

    ThreadLocal,叫做线程本地存储,也可以叫做线程本地变量.ThreadLocal为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量. 一.如何使用 class Acce ...

  8. Java核心复习——J.U.C LinkedBlockingQueue源码分析

    参考文档 LinkedBlockingQueue和ArrayBlockingQueue的异同

  9. Java核心复习——J.U.C ArrayBlockingQueue源码分析

    介绍 依赖关系 源码 构造方法 public ArrayBlockingQueue(int capacity) { this(capacity, false);//默认构造非公平的有界队列 } pub ...

随机推荐

  1. H5表单新特性

    1.HTML5表单新特性之——新的input type <input type=" "> HTML5之前已有的input type: text.password.rad ...

  2. sql server动态分页

    USE RYPlatformManagerDB GO SET ANSI_NULLS, QUOTED_IDENTIFIER ON GO CREATE Proc [dbo].[WEB_PageView] ...

  3. MySQL binlog反解析

    反解析delete语句 背景:delete table忘了加条件导致整张表被删除 恢复方式:直接从binlog里反解析delete语句为insert进行恢复 导出删指定表的DELETE语句: # my ...

  4. SDL图解

    1.什么是SDL 2.为什么要用SDL 3.SDL由哪几个阶段组成 用于规范公司web应用开发流程:安全需求分析.代码检查.安全测试... 4.微软的SDL实施流程

  5. centos6.x下让redis以服务方式运行

    1.从官网下载redis-2.8.9.tar.gz之后,将redis解压在/usr/local下,目录是redis-2.8.9,然后按照官网给出的办法安装redis即可. 2.安装完在redis-2. ...

  6. jQuery 中的 Ajax 方法(节选)

    $.ajax() 基本用法: $.ajax({ url: url, // 地址 data: data, // 参数 type: 'POST', // 提交方式 可以选择 post/get 推荐 pos ...

  7. re模块及其用法

    一.re模块下的常用方法 首先在使用re模块之前,需要引入re模块 import re 1.与查找相关的: 1.findall 返回列表,找到所有的匹配项 ret = re.findall(" ...

  8. js rsa sign使用笔记(加密,解密,签名,验签)

    你将会收获: js如何加密, 解密 js如何签名, 验签 js和Java交互如何相互解密, 验签(重点) 通过谷歌, 发现jsrsasign库使用者较多. 查看api发现这个库功能很健全. 本文使用方 ...

  9. [MVC] 自定义ActionSelector,根据参数选择Action[转载]

    很多时候我们会根据UI传入的参数,呈现不同的View.也就是对于同一个Action如何根据请求数据返回不同的View.通常情况下我们会按照如下方法来写,例如: [AcceptVerbs(HttpVer ...

  10. js 面向对象之属性描述符

    上回介绍了面向对象之构造器属性.这次介绍下属性描述符 遍历对象属性 let person = {name: "lisi"} for (key in person) { consol ...