Java核心复习——CompletableFuture
介绍
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的更多相关文章
- Java核心复习——J.U.C AbstractQueuedSynchronizer
第一眼看到AbstractQueuedSynchronizer,通常都会有这几个问题. AbstractQueuedSynchronizer为什么要搞这么一个类? 这个类是干什么的.有什么用? 这个类 ...
- Java核心复习——线程池ThreadPoolExecutor源码分析
一.线程池的介绍 线程池一种性能优化的重要手段.优化点在于创建线程和销毁线程会带来资源和时间上的消耗,而且线程池可以对线程进行管理,则可以减少这种损耗. 使用线程池的好处如下: 降低资源的消耗 提高响 ...
- Java核心复习—— 原子性、有序性与Happens-Before
一. 产生并发Bug的源头 可见性 缓存导致的可见性问题 原子性 线程切换带来的原子性问题 有序性 编译优化带来的有序性问题 上面讲到了 volatile 与可见性,本章再主要讲下原子性.有序性与Ha ...
- Java核心复习 —— J.U.C 并发工具类
一.CountDownLatch 文档描述 A synchronization aid that allows one or more threads to wait until* a set of ...
- Java核心复习——synchronized
一.概念 利用锁机制实现线程同步,synchronized关键字的底层交由了JVM通过C++来实现 Java中的锁有两大特性: 互斥性 同一时间,只允许一个线程持有某个对象锁. 可见性 锁释放前,线程 ...
- Java核心复习—— volatile 与可见性
一.介绍 volatile保证共享变量的"可见性".可见性指的是当一个线程修改变量时,另一个线程能读到这个修改的值. 这里就要提出几个问题. 问题1:为什么一个线程修改时,另一个线 ...
- Java核心复习—— ThreadLocal源码分析
ThreadLocal,叫做线程本地存储,也可以叫做线程本地变量.ThreadLocal为变量在每个线程中都创建了一个副本,那么每个线程可以访问自己内部的副本变量. 一.如何使用 class Acce ...
- Java核心复习——J.U.C LinkedBlockingQueue源码分析
参考文档 LinkedBlockingQueue和ArrayBlockingQueue的异同
- Java核心复习——J.U.C ArrayBlockingQueue源码分析
介绍 依赖关系 源码 构造方法 public ArrayBlockingQueue(int capacity) { this(capacity, false);//默认构造非公平的有界队列 } pub ...
随机推荐
- mysql存储过程事务
之前在写一个存储过程的时候由于不仔细导致数据库锁死,这里反省一下. 存储过程是这样的:把数据按顺序插入三张表,如果其中任何一处出错,就把前面已经做了的操作进行回滚,存储过程里面是用事务实现的,我是这么 ...
- Centos7.6进入挂载硬盘后,进入应急模式(emergency mode)而非图形模式解决方法
Centos7.6进入挂载硬盘后,进入应急模式(emergency mode)而非图形模式解决方法 话说某天我想在centos7.6中挂载个硬盘,结果刚在虚拟机中添加了一块硬盘,再次打开系统时,居然就 ...
- 运行 jcontrol 报 libXext.so.6: cannot open shared object file 错误
需要安装额外库: yum install libXext.x86_64 yum install libXrender.x86_64 yum install libXtst.x86_64
- vue 打印html
1.https://github.com/xyl66/vuePlugs_printjs从这个路径下载print.js.放到你的代码中 2.我是放到我本地一个js文件中. 3.引入当前文件 //打印插件 ...
- SpringCloud之Eureka
[前面的话]SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它配置简单,上手快,而且生态成 ...
- html知识补充
1.点击超链接跳转到新窗口 <a href="http://www.baidu.com" target="_blank">百度一下</a> ...
- P2002 消息扩散[SCC缩点]
题目描述 有n个城市,中间有单向道路连接,消息会沿着道路扩散,现在给出n个城市及其之间的道路,问至少需要在几个城市发布消息才能让这所有n个城市都得到消息. 输入格式 第一行两个整数n,m表示n个城市, ...
- linux下环境管理anaconda3
我之前在centos之安装单独python3.6,大家都知道centos自带python2.7,通过输入python,和python3来控制想要使用python2,或者python3,如今想要要在li ...
- C# String 字符拼接测试(“+”、string.Format、StringBuilder 比较)
对于字符串的拼接自己一直有疑问,在何时该用什么方法来拼接?哪种方法更好.更适合. 几种方法 1.“+” 拼接字符串 现在在 C# 中,字符串进行拼接,可以直接用 “+” 而且可以直接用于数字类型的而不 ...
- 一个s的力量——http与https
我们网络上有很多资源,那么我们怎么来访问呢?就拿一个小例子来说,我们都会用百度来搜索东西,就会在地址栏中输入这样的地址http://www.baidu.com这种访问方式就是通过URL来实现的,那么什 ...