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/ 瓜娃啊瓜娃
随机推荐
- command_execution
前置知识 可以通过ping的TTL来判断系统的版本 判断了是Linux之后就使用Linux的连接命令来进行操作 这里直接全局搜索flag相关的文件 linux全局查询文件_linux全局查找某个文件- ...
- .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
前言 我相信做开发的同学应该都对接过各种各样的第三方平台的登录授权,来获取用户信息(如:微信登录.支付宝登录.QQ登录.GitHub登录等等).今天给大家推荐一个.NET开源好用的.全面的.方便第三方 ...
- windows10 使用 USB 无线网卡的热点功能
一.概述 在某宝上买了一个 COMFAST CF-727B 的无线模块,由于笔记本电脑一直使用不上,所以放了很久.多年后我来到了一个公司,遇到了我此生最想吐槽的网管,简直不敢想象几十人的办公室,居然能 ...
- JS文本换行算法-模拟计算文字换行位置-基于DOM元素自发换行行为和字符分割原理-支持实体编码、不支持标签嵌套和富文本
简介之前在学习HTML的时候一直很想弄清楚HTML内部换行的逻辑,特别是有时候我们想知道一个字符串放入一个DOM元素之后究竟在哪个字符位发生的换行,然后就可以知道在一个固定宽高且隐藏溢出的容器中当前用 ...
- Java并发(十六)----线程八锁
所谓的"线程八锁" 其实就是看 synchronized 锁住的是哪个对象 情况1:12 或 21都是有可能的,就看cpu先调度哪个线程 @Slf4j(topic = " ...
- .NET 程序员-开源项目【藏】
Json.NET http://json.codeplex.com/ Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Lin ...
- Fragment动态添加与管理
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- 打造一个极度舒适的Chrome扩展项目开发环境
大家好,我是 dom 哥.这是我关于 Chrome 扩展开发的系列文章,感兴趣的可以 点个小星星. Chrome 扩展能够提高浏览器的使用体验,通过自定义 UI 界面,监听浏览器事件,改变 Web 页 ...
- ThreadLocal底层源码解析
ThreadLocal底层源码解析 ThreadLocal:顾名思义的意思是本地线程或者局部线程的意思,其真正含义是希望多个线程之间拥有自己的局部变量,多个线程间拥有自己的私人变量,在多线程间不被共享 ...
- 【C#】【字符串内插】关于$" "(字符串内插构造格式化字符串)的使用
1.变量名插入使用 var num = 1; Console.WriteLine($"Output number:{num}"); // Output: Output number ...