当前位置:网站首页>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
边栏推荐
猜你喜欢
整型int的拼接和拆分
Force buckle 599 Minimum index sum of two lists
With st7008, the Bluetooth test is completely grasped
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
【哲思与实战】程序设计之道
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
OneSpin | 解决IC设计中的硬件木马和安全信任问题
Opencv learning notes high dynamic range (HDR) imaging
Micro service remote debug, nocalhost + rainbow micro service development second bullet
一. 基础概念
随机推荐
php 获取图片信息的方法
VMWare中虚拟机网络配置
TS快速入门-泛型
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Spark 判断DF为空
图扑数字孪生煤矿开采系统,打造采煤“硬实力”
Kubernetes -- detailed usage of kubectl command line tool
Cuda版本不一致,编译apex报错
Solve the problem that the executable file of /bin/sh container is not found
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
备份 TiDB 集群到持久卷
恢复持久卷上的备份数据
力扣 2319. 判断矩阵是否是一个 X 矩阵
Splicing and splitting of integer ints
九度 1201 -二叉排序数遍历- 二叉排序树「建议收藏」
Force buckle 599 Minimum index sum of two lists
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
How C language determines whether it is a 32-bit system or a 64 bit system
Mongodb learn from simple to deep
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?