当前位置:网站首页>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
边栏推荐
- 【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
- 【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
- Solve the problem of incomplete display around LCD display of rk3128 projector
- [philosophy and practice] the way of program design
- 第二十章 使用工作队列管理器(三)
- ASP. Net learning & ASP's one word
- 有了ST7008, 蓝牙测试完全拿捏住了
- Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
- 有用的win11小技巧
- 大厂经典指针笔试题
猜你喜欢
随机推荐
Solve the problem that the executable file of /bin/sh container is not found
Force buckle 674 Longest continuous increasing sequence
力扣599. 两个列表的最小索引总和
Graduation season | regretful and lucky graduation season
恢复持久卷上的备份数据
VMWare中虚拟机网络配置
Traversal of Oracle stored procedures
ASP. Net learning & ASP's one word
I Basic concepts
Implement secondary index with Gaussian redis
Mongodb learn from simple to deep
整型int的拼接和拆分
Get webkitformboundary post login
[MySQL - Basic] transactions
取两个集合的交集
上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法
Read PG in data warehouse in one article_ stat
c语言如何判定是32位系统还是64位系统
基于深度学习的目标检测的更新迭代总结(持续更新ing)
Spark 判断DF为空