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/ 瓜娃啊瓜娃
随机推荐
- 记一次 .NET 某券商论坛系统 卡死分析
一:背景 1. 讲故事 前几个月有位朋友找到我,说他们的的web程序没有响应了,而且监控发现线程数特别高,内存也特别大,让我帮忙看一下怎么回事,现在回过头来几经波折,回味价值太浓了. 二:程序到底经历 ...
- Echarts 饼图,legend样式美化
最后样式图: 实现代码: var myChart = echarts.init(document.getElementById('container')); let option = { /*{b}: ...
- 提升开发技能:10个高级的JavaScript技巧
前言 在这个快速发展的数字时代,JavaScript作为一种广泛应用的编程语言,其重要性愈发凸显.为了在竞争激烈的开发领域中保持竞争力,不断提升自己的技能是至关重要的.本文小编将您介绍10个高级的Ja ...
- Java核心知识体系7:线程安全性讨论
Java核心知识体系1:泛型机制详解 Java核心知识体系2:注解机制详解 Java核心知识体系3:异常机制详解 Java核心知识体系4:AOP原理和切面应用 Java核心知识体系5:反射机制详解 J ...
- 2023年奔走的总结---吉特日化MES 项目趣事 篇一
终于又到了2023年的年末了,既有兴奋也有遗憾,兴奋的是这难熬的一年马上就要过去了,遗憾的是今年好像没有做成太多的事情.差不多180次的大交通记录,也再次刷新了奔走的记录,忙忙碌碌兜兜转转又去了不少地 ...
- [ABC246A] Four Points
Problem Statement There is a rectangle in the $xy$-plane. Each edge of this rectangle is parallel to ...
- vue-test4 -----插槽
<template> <!-- <Main class="cccc"/> <component-a/> --> <slot-d ...
- Java8新特性Stream流
1.是什么? Stream(流)是一个来自数据源的元素队列并支持聚合操作 2.能干嘛? Stream流的元素是特定类型的对象,形成一个队列. Java中的Stream并不会存储元素,而是按需计算. 数 ...
- ezbypass-cat【目录穿透】
ezbypass-cat[目录穿透][难度:4] 题目界面 尝试SQL注入无果,看题解发现是目录穿透,需要抓包才能得到flag. 解题步骤 随便输入用户名和密码抓包 更改请求方式(POST→GET), ...
- Linux 485驱动通信异常
背景 前段时间接到一个项目,要求用主控用485和MCU通信.将代码调试好之后,验证没问题就发给测试了.测试测的也没问题. 但是,到设备量产时,发现有几台设备功能异常.将设备拿回来排查,发现是485通信 ...