当前位置:网站首页>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
边栏推荐
- Opencv学习笔记 高动态范围 (HDR) 成像
- Implement secondary index with Gaussian redis
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- 一键部署Redis任意版本
- Splicing and splitting of integer ints
- How C language determines whether it is a 32-bit system or a 64 bit system
- 使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
- rk3128投影仪lcd显示四周显示不完整解决
- Meta Force原力元宇宙系统开发佛萨奇模式
- Network principle (1) - overview of basic principles
猜你喜欢
如何满足医疗设备对安全性和保密性的双重需求?
ISO 26262 - 基于需求测试以外的考虑因素
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统
CodeSonar网络研讨会
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
Apifox interface integrated management new artifact
机械臂速成小指南(十二):逆运动学分析
一. 基础概念
Implement secondary index with Gaussian redis
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
随机推荐
Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
一键部署Redis任意版本
Graduation season | regretful and lucky graduation season
Splicing and splitting of integer ints
Make this crmeb single merchant wechat mall system popular, so easy to use!
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
POJ 1742 coins (monotone queue solution) [suggestions collection]
Network principle (1) - overview of basic principles
【哲思与实战】程序设计之道
Flask1.1.4 Werkzeug1.0.1 源码分析:路由
H3C s7000/s7500e/10500 series post stack BFD detection configuration method
How to implement safety practice in software development stage
开发一个小程序商城需要多少钱?
机械臂速成小指南(十一):坐标系的标准命名
华为CE交换机下载文件FTP步骤
CJSON内存泄漏的注意事项
Apifox 接口一体化管理新神器
理财产品要怎么选?新手还什么都不懂
kubernetes之创建mysql8