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 ...
随机推荐
- 【转载】C#编程中两个List集合使用Intersect方法求交集
在C#语言程序设计中,List集合是常用的集合数据类型,在涉及集合类型的运算中,有时候我们需要计算2个List集合中共有的数据,即对2个List集合求交集运算.此时可以使用C#语言提供的Interse ...
- falcon 数据丢失处理方法参考
背景:使用 netstat -ano | grep ESTABLISH | grep 11883 监控 send_Q 和 recv_Q 的值,由于单次推送数据量在1w条以上,导致falcon-agen ...
- table固定宽度与自动宽度
table-layout:auto(创建的table默认是此布局模式): 对table和td.th指定的宽度无效,浏览器会计算所有单元格的内容宽度才能得出一列宽度 如果想对单元格的内容自动折行需使用w ...
- VUe键盘修饰符及自定义指令获取焦点
首先需要在keyup事件之后. 修饰符 来绑定事件 <body> <div class="box"> <!-- 这里的 @keyup.enter=&q ...
- 在使用pandas获取网上数据报出url错误的解决办法
在使用pandas.read_csv('网址名')时,出现url错误是,需要在导包出添加一下两句代码 import ssl ssl._create_default_https_context = ss ...
- 使用 rem 设置文字大小
一.那到底什么是 rem 呢? 规范中明确写道: Equal to the computed value of ‘font-size’ on the root element. 「rem」是指根元素( ...
- Codeforces #369 (Div. 2) C. Coloring Trees (3维dp
http://codeforces.com/group/1EzrFFyOc0/contest/711/problem/C https://blog.csdn.net/qq_36368339/artic ...
- python3 jieba分词
一.jieba库用于分词,https://github.com/fxsjy/jieba 二.分词:分词精细:全局(文本分析)<精确(快速成词)<搜素(搜素引擎分词) #分词 str=r'今 ...
- Goodbye Microservices: From 100s of problem children to 1 superstar
https://segment.com/blog/goodbye-microservices/ Unless you’ve been living under a rock, you probably ...
- Selenium(十三)调用js,控制浏览器的滚动条
WebDiver 不能操作本地 Windows 控件,但对于浏览器上的控件也不是都可以操作的.比如浏览器的滚动条,虽然 WebDriver 提供操作浏览器的前进和后退按钮,但对于滚动条并没有提供相应用 ...