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的更多相关文章

  1. 初探Google Guava

    Guava地址:https://github.com/google/guava 第一次接触我是在16年春github上,当时在找单机查缓存方法,google guava当初取名是因为JAVA的类库不好 ...

  2. java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)

    学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...

  3. [转]Google Guava官方教程(中文版)

    Google Guava官方教程(中文版) http://ifeve.com/google-guava/

  4. Google Guava官方教程(中文版)

    Google Guava官方教程(中文版) 原文链接  译文链接 译者: 沈义扬,罗立树,何一昕,武祖  校对:方腾飞 引言 Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库, ...

  5. 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. ...

  6. 使用 Google Guava 美化你的 Java 代码

    文章转载自:http://my.oschina.net/leejun2005/blog/172328 目录:[ - ] 1-使用 GOOGLE COLLECTIONS,GUAVA,STATIC IMP ...

  7. Google Guava之--cache

    一.简介 Google Guava包含了Google的Java项目许多依赖的库,如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support ...

  8. Google Guava学习笔记——简介

    Google Guava是什么东西?首先要追溯到2007年的“Google Collections Library”项目,它提供对Java 集合操作的工具类.后来Guava被进化为Java程序员开发必 ...

  9. (翻译)Google Guava Cache

    翻译自Google Guava Cache This Post is a continuation of my series on Google Guava, this time covering G ...

  10. Google Guava官方教程(中文版)地址

    Google Guava官方教程(中文版) http://ifeve.com/google-guava/ 瓜娃啊瓜娃

随机推荐

  1. Vue源码学习(十五):diff算法(二)交叉比对(双指针)

    好家伙, 本节来解决我们上一章留下来的问题, 新旧节点同时有儿子的情况本章继续解决 1.要做什么? 本章将解决, 1.在相同tag下子元素的替换问题 2.使用双指针进行元素替换, 实现效果如下:   ...

  2. 快速入门:构建您的第一个 .NET Aspire 应用程序

    前言 云原生应用程序通常需要连接到各种服务,例如数据库.存储和缓存解决方案.消息传递提供商或其他 Web 服务..NET Aspire 旨在简化这些类型服务之间的连接和配置.在本快速入门中,您将了解如 ...

  3. Codeforces Round #700 (Div. 2) A~C题解

    写在前边 链接:Codeforces Round #699 (Div. 2) A. Yet Another String Game 链接:A题链接 题目大意: 给定一个字符串,有两位同学来操作这个字符 ...

  4. Android 11 使用 BroadcastReceiver 监听短消息

    使用装有MIUI系统的小米手机,静态注册的广播接收器监听短消息. 在AndroidManifest.xml中声明权限 <uses-permission android:name="an ...

  5. kubernetes container device interface (CDI)

    CDI 是什么? Container Device Interface (CDI) 是一个提议的标准,它定义了如何在容器运行时环境中向容器提供设备.这个提议的目的是使得设备供应商能够更容易地将其设备集 ...

  6. Python有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

    n = 0 for i in range(1, 5): for j in range(1, 5): for k in range(1, 5): if(i != k) and (i != j) and ...

  7. 【外包杯】【无语的报错】意想不到的逗号Unexpected comma.(已解决)

    解决了,答案是没保存,看见那些文件是型号了吗,就是这个原因.

  8. localhost工具:本地代码的远程之路

    在日常的开发过程中,本地代码远程调试一直是最理想的开发状态.本文通过介绍京东集团内开发的一个轻量简单的小工具"localhost",从多角度的方案思考,到原理介绍,到最终的方案落地 ...

  9. [清华集训2017] Hello World!

    Hello world! 题目背景 不远的一年前,小 V 还是一名清华集训的选手,坐在机房里为他已如风中残烛的OI 生涯做最后的挣扎.而如今,他已成为了一名光荣的出题人.他感到非常激动,不禁感叹道: ...

  10. 基于.NetCore开发博客项目 StarBlog - (30) 实现评论系统

    前言 时隔五个月,终于又来更新 StarBlog 系列了~ 这次是呼声很大的评论系统. 由于涉及的代码量比较大,所以本文不会贴出所有代码,只介绍关键逻辑,具体代码请同学们自行查看 GitHub 仓库. ...