当前位置:网站首页>Guava multithreading, futurecallback thread calls are uneven
Guava multithreading, futurecallback thread calls are uneven
2022-07-07 20:25:00 【zzhongcy】
Recent view log , Find out guava The multiple futurecallback Thread calls are uneven , Record it here
Guava Futures brief introduction
Google Guava Framework of the com.google.common.util.concurrent Packages are concurrency related packages , It's right JDK Bring their own
concurrent In bag Future Extensions of classes related to thread pool , Thus, some new classes are derived , And provides a wider range of functions .
The classes commonly used in this package in the project are as follows :
- ListenableFuture: The interface extends Future Interface , Added addListener Method , This method is used in a given excutor Register a monitor on , When the calculation is completed, the listener will be called immediately . The sequence of listener execution cannot be ensured , But you can ensure that it is called immediately when the calculation is completed .
- FutureCallback: This interface provides OnSuccess and onFailure Method . Get the result of asynchronous calculation and call back .
- MoreExecutors: This category is final Tool class of type , Provides a lot of static methods . for example listeningDecorator Method initialization ListeningExecutorService Method , Using this example submit Method to initialize ListenableFuture object .
- ListenableFutureTask: This class is an adapter , Others can be Future Adaptation ListenableFuture.
- ListeningExecutorService: This category is right ExecutorService An extension of , Rewrote ExecutorService Class submit Method , And back to ListenableFuture object .
- JdkFutureAdapters: This class extends FutureTask Class and Implementation ListenableFuture Interface , Added addListener Method .
- Futures: This class provides many practical static methods for use .
The phenomenon
I created 8 Threads , Average multi task processing , But at the end of the day futurecallback The thread segments called are uneven .
journal :
2022-06-10 16:49:23.402 [pool-7-thread-7] INFO c.l.AsyncTask - percent=4%
2022-06-10 16:49:23.402 [pool-7-thread-7] INFO c.l.AsyncTask - percent=8%
2022-06-10 16:49:23.402 [pool-7-thread-7] INFO c.l.AsyncTask - percent=12%
2022-06-10 16:49:23.407 [pool-7-thread-7] INFO c.l.AsyncTask - percent=16%
2022-06-10 16:49:23.407 [pool-7-thread-7] INFO c.l.AsyncTask - percent=20%
2022-06-10 16:49:23.409 [pool-7-thread-7] INFO c.l.AsyncTask - percent=24%
2022-06-10 16:49:23.409 [pool-7-thread-7] INFO c.l.AsyncTask - percent=28%
2022-06-10 16:49:23.409 [pool-7-thread-7] INFO c.l.AsyncTask - percent=32%
2022-06-10 16:49:23.411 [pool-7-thread-7] INFO c.l.AsyncTask - percent=36%
2022-06-10 16:49:23.411 [pool-7-thread-7] INFO c.l.AsyncTask - percent=40%
2022-06-10 16:49:23.413 [pool-7-thread-7] INFO c.l.AsyncTask - percent=44%
2022-06-10 16:49:23.413 [pool-7-thread-7] INFO c.l.AsyncTask - percent=48%
2022-06-10 16:49:23.413 [pool-7-thread-7] INFO c.l.AsyncTask - percent=52%
2022-06-10 16:49:23.416 [pool-7-thread-7] INFO c.l.AsyncTask - percent=56%
2022-06-10 16:49:23.416 [pool-7-thread-7] INFO c.l.AsyncTask - percent=60%
2022-06-10 16:49:23.418 [pool-7-thread-7] INFO c.l.AsyncTask - percent=64%
2022-06-10 16:49:23.418 [pool-7-thread-7] INFO c.l.AsyncTask - percent=68%
2022-06-10 16:49:23.418 [pool-7-thread-7] INFO c.l.AsyncTask - percent=72%
2022-06-10 16:49:25.231 [pool-7-thread-8] INFO c.l.AsyncTask - percent=76%
2022-06-10 16:49:27.436 [pool-7-thread-2] INFO c.l.AsyncTask - percent=80%
2022-06-10 16:49:27.970 [pool-7-thread-4] INFO c.l.AsyncTask - percent=84%
2022-06-10 16:49:31.812 [pool-7-thread-5] INFO c.l.AsyncTask - percent=88%
2022-06-10 16:49:33.475 [pool-7-thread-6] INFO c.l.AsyncTask - percent=92%
2022-06-10 16:49:34.184 [pool-7-thread-3] INFO c.l.AsyncTask - percent=96%
2022-06-10 16:56:56.214 [pool-7-thread-1] INFO c.l.AsyncTask - percent=100%
For example :
ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(false);
return t;
}
});
ListenableFuture<?> listenableFuture = JdkFutureAdapters.listenInPoolThread(
executorService.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("run!!!");
}
}),
executorService
);
Futures.addCallback(listenableFuture, new FutureCallback<Object>() {
@Override
public void onSuccess(@Nullable Object result) {
System.out.println("onSuccess");
}
@Override
public void onFailure(Throwable t) {
System.out.println("onFailure");
}
});Reference resources
Futures (Guava: Google Core Libraries for Java 23.0 API)
https://stackoverflow.com/questions/63629421/listenable-future-callback-is-heavily-delayed
边栏推荐
- CodeSonar通过创新型静态分析增强软件可靠性
- Small guide for rapid formation of manipulator (12): inverse kinematics analysis
- Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
- Force buckle 1232 Dotted line
- 一键部署Redis任意版本
- 基于深度学习的目标检测的更新迭代总结(持续更新ing)
- Flask1.1.4 Werkzeug1.0.1 源码分析:路由
- H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
- Yolov6:yolov6+win10--- train your own dataset
- 一. 基础概念
猜你喜欢

Mongodb learn from simple to deep

I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!

智能软件分析平台Embold
![[philosophy and practice] the way of program design](/img/c8/93f2ac7c5beb95f64b7883ad63c74c.jpg)
[philosophy and practice] the way of program design

【哲思与实战】程序设计之道

Mongodb由浅入深学习

Micro service remote debug, nocalhost + rainbow micro service development second bullet

Dachang classic pointer written test questions

机械臂速成小指南(十二):逆运动学分析

机器学习笔记 - 使用Streamlit探索对象检测数据集
随机推荐
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
Graduation season | regretful and lucky graduation season
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
Solve the problem of incomplete display around LCD display of rk3128 projector
Gorilla official: sample code for golang to open websocket client
Opencv learning notes high dynamic range (HDR) imaging
寫一下跳錶
【mysql篇-基础篇】事务
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
网络原理(1)——基础原理概述
How to implement safety practice in software development stage
【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
字符串中数据排序
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
MSE API learning
机械臂速成小指南(十一):坐标系的标准命名
九度 1201 -二叉排序数遍历- 二叉排序树「建议收藏」
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
怎样用Google APIs和Google的应用系统进行集成(1)—-Google APIs简介
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!