当前位置:网站首页>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
边栏推荐
- CIS芯片测试到底怎么测?
- Mrs offline data analysis: process OBS data through Flink job
- [solution] package 'XXXX' is not in goroot
- FTP steps for downloading files from Huawei CE switches
- 【解决】package ‘xxxx‘ is not in GOROOT
- How C language determines whether it is a 32-bit system or a 64 bit system
- Yolov6:yolov6+win10--- train your own dataset
- 机械臂速成小指南(十二):逆运动学分析
- Force buckle 599 Minimum index sum of two lists
- Splicing and splitting of integer ints
猜你喜欢
Network principle (1) - overview of basic principles
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
如何满足医疗设备对安全性和保密性的双重需求?
ISO 26262 - 基于需求测试以外的考虑因素
Vulnhub's funfox2
CIS芯片测试到底怎么测?
Implement secondary index with Gaussian redis
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
php 获取图片信息的方法
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
随机推荐
阿里云有奖体验:如何通过ECS挂载NAS文件系统
一键部署Redis任意版本
使用 BR 恢复 Azure Blob Storage 上的备份数据
How C language determines whether it is a 32-bit system or a 64 bit system
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
备份 TiDB 集群到持久卷
解决/bin/sh进去的容器运行可执行文件报not found的问题
Read PG in data warehouse in one article_ stat
Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
九度 1201 -二叉排序数遍历- 二叉排序树「建议收藏」
CodeSonar如何帮助无人机查找软件缺陷?
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
【解决】package ‘xxxx‘ is not in GOROOT
Spark judges that DF is empty
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
Nebula Importer 数据导入实践
寫一下跳錶
Splicing and splitting of integer ints
Yolov6:yolov6+win10--- train your own dataset
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统