Google Guava ListeningExecutorService
POM
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
package com.vipsoft; import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.*; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*; public class ListeningExecutorTest { /**
* 线程池
*/
static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 10, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(200),
new ThreadPoolExecutor.CallerRunsPolicy()
); /**
* 数据处理
*
* @return
* @throws Exception
*/ public static void main(String[] args) throws Exception {
List<String> result = new ArrayList<>();
List<String> list = new ArrayList<>(); //模拟原始数据
for (int i = 0; i < 1211; i++) {
list.add(i + "-");
System.out.println("添加原始数据:" + i);
} int size = 50;//切分粒度,每size条数据,切分一块,交由一条线程处理
int countNum = 0;//当前处理到的位置
int count = list.size() / size;//切分块数
int threadNum = 0;//使用线程数
if (count * size != list.size()) {
count++;
} final CountDownLatch countDownLatch = new CountDownLatch(count); //使用Guava的ListeningExecutorService装饰线程池
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(threadPoolExecutor); while (countNum < count * size) {
//切割不同的数据块,分段处理
threadNum++;
countNum += size;
MyCallable myCallable = new MyCallable();
myCallable.setList(ImmutableList.copyOf(list.subList(countNum - size, list.size() > countNum ? countNum : list.size()))); ListenableFuture listenableFuture = executorService.submit(myCallable); //回调函数
Futures.addCallback(listenableFuture, new FutureCallback<List<String>>() {
//任务处理成功时执行
@Override
public void onSuccess(List<String> list) {
countDownLatch.countDown();
System.out.println("第h次处理完成");
result.addAll(list);
} //任务处理失败时执行
@Override
public void onFailure(Throwable throwable) {
countDownLatch.countDown();
System.out.println("处理失败:" + throwable);
}
},executorService ); } //设置时间,超时了直接向下执行,不再阻塞
countDownLatch.await(3, TimeUnit.SECONDS); result.stream().forEach(s -> System.out.println(s));
System.out.println("------------结果处理完毕,返回完毕,使用线程数量:" + threadNum);
} static class MyCallable implements Callable { private List<String> list; @Override
public Object call() throws Exception {
List<String> listReturn = new ArrayList<>();
//模拟对数据处理,然后返回
for (int i = 0; i < list.size(); i++) {
listReturn.add(list.get(i) + ":处理时间:" + System.currentTimeMillis() + "---:处理线程:" + Thread.currentThread());
} return listReturn;
} public void setList(List<String> list) {
this.list = list;
}
}
}
Google Guava ListeningExecutorService的更多相关文章
- 初探Google Guava
Guava地址:https://github.com/google/guava 第一次接触我是在16年春github上,当时在找单机查缓存方法,google guava当初取名是因为JAVA的类库不好 ...
- java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)
学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...
- [转]Google Guava官方教程(中文版)
Google Guava官方教程(中文版) http://ifeve.com/google-guava/
- Google Guava官方教程(中文版)
Google Guava官方教程(中文版) 原文链接 译文链接 译者: 沈义扬,罗立树,何一昕,武祖 校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...
- Google Guava vs Apache Commons for Argument Validation
It is an established good practice to validate method arguments at the beginning of the method body. ...
- 使用 Google Guava 美化你的 Java 代码
文章转载自:http://my.oschina.net/leejun2005/blog/172328 目录:[ - ] 1-使用 GOOGLE COLLECTIONS,GUAVA,STATIC IMP ...
- Google Guava之--cache
一.简介 Google Guava包含了Google的Java项目许多依赖的库,如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support ...
- Google Guava学习笔记——简介
Google Guava是什么东西?首先要追溯到2007年的“Google Collections Library”项目,它提供对Java 集合操作的工具类.后来Guava被进化为Java程序员开发必 ...
- (翻译)Google Guava Cache
翻译自Google Guava Cache This Post is a continuation of my series on Google Guava, this time covering G ...
- Google Guava官方教程(中文版)地址
Google Guava官方教程(中文版) http://ifeve.com/google-guava/ 瓜娃啊瓜娃
随机推荐
- Calendar日历类型常见方法(必看!!)
Hi i,m JinXiang 前言 本篇文章主要介绍Calendar日历类型的几种常见方法以及部分理论知识 欢迎点赞 收藏 留言评论 私信必回哟 博主收将持续更新学习记录获,友友们有任何问题可以在 ...
- 三菱PLC 轻松数采
目前市面上数采的软件有很多,但是用的最为省力最为简单的就是kepserver了,在kepserver的应用中,有对应的三菱驱动针对于三菱PLC,三菱驱动支持多个Mitsubishi 协议,包括 MEL ...
- 【uniapp】【外包杯】学习笔记day05 | 页面制作+git安装+云存储
没啥好说的,昨天的任务没有完成,网上说的是一套,我这边出现的情况又是一种,欸...善莫大焉 今天又重新开始搞,有了一点起色,然后用了git进行云存储 明天计划把这个给搞定吧,累发财了 git做完了 在 ...
- 初探webpack之单应用多端构建
初探webpack之单应用多端构建 在现代化前端开发中,我们可以借助构建工具来简化很多工作,单应用多端构建就是其中应用比较广泛的方案,webpack中提供了loader与plugin来给予开发者非常大 ...
- 深度解析C#中LinkedList<T>的存储结构
本文承接前面的3篇有关C#的数据结构分析的文章,对于C#有关数据结构分析还有一篇就要暂时结束了,这个系列主要从Array.List.Dictionary.LinkedList. SortedSet等5 ...
- 【软件安装】vmware虚拟机安装完整教程(15.5版本)
目录 一.基础介绍 二.准备工作(注意:如果自己下载不下来翻到最下面获取下载地址) 三.VMware下载与安装 VMware Workstation15.5新功能 注意: 一.基础介绍 VMware ...
- C++学习笔记五:变量与数据类型(Auto类型)
Auto 允许编译器自己来推断变量的类型,这种新功能是在c++11引入的.这个关键字结合for循环使用可以节省变量类型的重复输入.VS Code可以在鼠标移动到变量上之后直接显示变量的类型. auto ...
- offline RL | Pessimistic Bootstrapping (PBRL):在 Q 更新中惩罚 uncertainty,拉低 OOD Q value
论文题目:Pessimistic Bootstrapping for Uncertainty-Driven Offline Reinforcement Learning,ICLR 2022,6 6 8 ...
- AI助力软件工程师高效工作:8款神器助你优化工作流程
随着人工智能技术的不断发展,AI工具在软件工程领域展现出强大的应用潜力.善用 AI 工具可以消除繁琐事务带来的倦怠,帮助软件工程师更好地传达想法,完成更高质量的工作.我们可以将 AI 以各种方式应用于 ...
- 华企盾DSC苹果电脑-认证用户提示“不是认证成功的账户”
出现该问题说明客户端连不上服务器,一般来说是网络原因,可按照下面方法排查 1.先检查网络是否通能否正常上网或者换一个网络试试 2.查看服务器是否启动 3.客户测试连接是否成功端口是否填成了5580 ...