当前位置:网站首页>guava多线程,futurecallback线程调用不平均
guava多线程,futurecallback线程调用不平均
2022-07-07 18:22:00 【zzhongcy】
最近查看日志,发现guava的多个futurecallback线程调用不平均,在这里记录一下
Guava Futures简介
Google Guava框架的 com.google.common.util.concurrent包是并发相关的包,它是对JDK自带
concurrent包中Future和线程池相关类的扩展,从而衍生出一些新类,并提供了更为广泛的功能。
在项目中常用的该包中类如下所示:
- ListenableFuture:该接口扩展了Future接口,增加了addListener方法,该方法在给定的excutor上注册一个监听器,当计算完成时会马上调用该监听器。不能够确保监听器执行的顺序,但可以在计算完成时确保马上被调用。
- FutureCallback:该接口提供了OnSuccess和onFailure方法。获取异步计算的结果并回调。
- MoreExecutors:该类是final类型的工具类,提供了很多静态方法。例如listeningDecorator方法初始化ListeningExecutorService方法,使用此实例submit方法即可初始化ListenableFuture对象。
- ListenableFutureTask:该类是一个适配器,可以将其它Future适配成ListenableFuture。
- ListeningExecutorService:该类是对ExecutorService的扩展,重写了ExecutorService类中的submit方法,并返回ListenableFuture对象。
- JdkFutureAdapters:该类扩展了FutureTask类并实现ListenableFuture接口,增加了addListener方法。
- Futures:该类提供和很多实用的静态方法以供使用。
现象
我创建了8个线程,平均处理多任务,但是到最后发现futurecallback调用的线程分部不平均。
日志:
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%
参考例子:
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");
}
});参考
Futures (Guava: Google Core Libraries for Java 23.0 API)
https://stackoverflow.com/questions/63629421/listenable-future-callback-is-heavily-delayed
边栏推荐
- 浅尝不辄止系列之试试腾讯云的TUIRoom(晚上有约,未完待续...)
- 《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
- Force buckle 599 Minimum index sum of two lists
- Data island is the first danger encountered by enterprises in their digital transformation
- Yolov6:yolov6+win10--- train your own dataset
- 【Auto.js】自动化脚本
- Force buckle 1961 Check whether the string is an array prefix
- 凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
- Implement secondary index with Gaussian redis
- Nebula Importer 数据导入实践
猜你喜欢

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

CodeSonar网络研讨会

The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?

AADL Inspector 故障树安全分析模块

Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation

使用高斯Redis实现二级索引

使用高斯Redis实现二级索引

Implement secondary index with Gaussian redis

Cantata9.0 | 全 新 功 能
![嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]](/img/af/61b384b1b6ba46aa1a6011f8a30085.png)
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
随机推荐
CJSON内存泄漏的注意事项
2022如何评估与选择低代码开发平台?
You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
Force buckle 674 Longest continuous increasing sequence
Mongodb由浅入深学习
Graduation season | regretful and lucky graduation season
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Force buckle 599 Minimum index sum of two lists
Implement secondary index with Gaussian redis
使用高斯Redis实现二级索引
Force buckle 989 Integer addition in array form
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
CUDA versions are inconsistent, and errors are reported when compiling apex
华为CE交换机下载文件FTP步骤
Opencv学习笔记 高动态范围 (HDR) 成像
《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
【Auto.js】自动化脚本
TS quick start - Generic
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
一键部署Redis任意版本