一种优化操作list、数组的多线程解决方案。
这几天接触到了一些操作list的功能,由于list太长,加上每条数据的处理时间,导致性能下降,正好利用学来的多线程知识和网上的资料结合实践一番,写出了一个通用类如下。
/**
* 操作数组的线程
*
* @author 80004133
*/
public abstract class OperateListThread{
/**
* 核心数组,用户需要操作的数组
*/
public Object[] arr;
private static final Logger logger = LoggerFactory.getLogger(OperateListThread.class);
/**
* 操作数组线程的数量,默认为5
*/
private int threadNum = 5;
ExecutorService exec;
public OperateListThread(List obj, int threadNum){
this.arr = obj.toArray();
this.threadNum = threadNum;
exec = Executors.newFixedThreadPool(threadNum+1);
}
/**
* 获取操作数组后的结果
* <p>
* 有返回结果时重写该方法
* </p>
* @return
*/
public Object getResult(){
return null;
};
/**
* 用户需要实现的方法
* <p>
* 代表用户希望做什么事情,该方法被run方法调用,需要用户实现
* </p>
*/
public abstract void doRun(int index);
/**
* 调用此方法开始工作
* @throws InterruptedException
*/
public void doWork() throws InterruptedException {
logger.info("Work start ------");
long start = System.currentTimeMillis();
int length = arr.length;
CountDownLatch latch = new CountDownLatch(arr.length % threadNum == 0 ? threadNum : threadNum+1);
logger.info("length:" + length + ":" + "latch:" + latch.getCount());
for (int j = 0; j < length; j += length / threadNum) {
MyThread m = null;
if ((j + (length / threadNum)) <= length) {
m = new MyThread(arr, j, j + length / threadNum, latch);
} else {
m = new MyThread(arr, j, length, latch);
}
exec.execute(m);
}
latch.await();
exec.shutdown();
logger.info("Spand time:" + (System.currentTimeMillis() - start));
logger.info("Work end -------");
}
public class MyThread implements Runnable {
Object[] arr;
int startIndex;
int endIndex;
CountDownLatch latch;
public MyThread(Object[] arr, int startIndex, int endIndex, CountDownLatch latch) {
this.arr = arr;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.latch = latch;
}
@Override
public void run() {
for (int i = startIndex; i < endIndex; i++) {
//要做的事
doRun(i);
}
logger.info(Thread.currentThread().getName());
latch.countDown();
}
}
public static void main(String[] args) throws InterruptedException{
List<Integer> arr = new ArrayList<>();
for (int i = 1; i <= 10000; i++) {
arr.add(i);
}
// int sum = 0;
// for (int a: arr) {
// sum += a;
// }
// System.out.println(sum);
OperateListThread op = new OperateListThread(arr, 5) {
public int sum = 0;
public Object getResult() {
return sum;
}
@Override
public void doRun(int index) {
sum += (int) arr[index];
}
};
op.doWork();
int result = (int)op.getResult();
System.out.println(result);
}
}
main方法举了一个很简单的使用实例,计算1+2+3+...+10000的和。这个通用类是一个抽象类,用法是实现这个抽象类,并只需要实现简单的doRun()方法,这个方法主要是对list做什么样的操作,index代表当前数组的位置,核心数组为arr。若你需要得到返回值,可以重写getResult()方法来获取返回值。
当然,有更好的方案或可以改正的地方欢迎联系我(QQ:2470244153),或在评论处留下您的留言。
一种优化操作list、数组的多线程解决方案。的更多相关文章
- [转]通俗易懂的php多线程解决方案
原文: https://www.w3cschool.cn/php/php-thread.html --------------------------------------------------- ...
- 绝对好文C#调用C++DLL传递结构体数组的终极解决方案
C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文 http://blog.csdn.net/xxdddail/art ...
- JS中几种常见的数组算法(前端面试必看)
JS中几种常见的数组算法 1.将稀疏数组变成不稀疏数组 /** * 稀疏数组 变为 不稀疏数组 * @params array arr 稀疏数组 * @return array 不稀疏的数组 */ f ...
- 一种使用pyinstaller时图标问题解决方案
一种使用pyinstaller时图标问题解决方案 0x00 场景 使用pyinstaller将.py文件编译成.exe文件时,想要使用自己心仪的图标(.ico)比较麻烦.在使用pyinstalle ...
- Numpy的ndarry:一种多维数组对象
Numpy的ndarry:一种多维数组对象 Numpy最重要的一个特点就是其N维数组对象(即ndarry),该对象是一个快速而灵活的大数据集容器.你可以利用这种数组对整块数据执行一些数学运算,其语法跟 ...
- Java并发和多线程2:3种方式实现数组求和
本篇演示3个数组求和的例子. 例子1:单线程例子2:多线程,同步求和(如果没有计算完成,会阻塞)例子3:多线程,异步求和(先累加已经完成的计算结果) 例子1-代码 package cn.fansuni ...
- lua多线程解决方案
直观的讲:lua并不支持多线程,lua语言本身具有携程功能,但携程仅仅是一种中继器. lua多线程的目的:有并发需求时,共享一些数据. 例如使用lua写一个并发服务器.用户登陆之后,用户数据储存在lu ...
- 几种php 删除数组元素方法
几种php教程 删除数组元素方法在很多情况下我们的数组会出现重复情况,那我们删除数组中一些重复的内容怎么办,这些元素我必须保持他唯一,所以就想办法来删除它们,下面利用了遍历查询来删除重复数组元素的几种 ...
- 堆的 两种实现 (数组和STL)
基本思想: 两种操作都跟树的深度成正比,所以复杂度 O(log(n)) ; push():在向堆中插入数值时,首先在堆的末尾插入该数值,然后不断向上提直到没有大小颠倒为止. pop(): 从堆中取出 ...
随机推荐
- pip安装模块使用国内镜像源加速安装
今天在安装Python模块matplotlib的时候,一直安装不成功,老是提示“socket.timeout: The read operation timed out”或者“Read timed o ...
- Python语言基础01-初识Python
本文收录在Python从入门到精通系列文章系列 1. Python简介 1.1 Python的历史 Python的创始人为吉多·范罗苏姆(荷兰语:Guido van Rossum) 1989年的圣诞节 ...
- HDU 1072 Nightmare 题解
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 简单C语言程序
#include<stdio.h> int main(void) { /* 计算两个时间的时间差 */ int hour1, minute1; int hour2, minute2; pr ...
- 解决chrome连接自建https服务器报“您的连接不是私密连接”问题
前一段时间,Chrome 突然显示出了“您的连接不是私密连接”,这下可难受了,大部分的网站打开都有问题. 找了各种方法,各种设置都是不行. 一.暴力.费力的方法直接卸载 Chrome ,删除一切数据以 ...
- JDOJ 1775: 求N!中0的个数
JDOJ 1775: 求N!中0的个数 JDOJ传送门 Description 求N!结果中末尾0的个数 N! = 1 * 2 * 3 ....... N Input 输入一行,N(0 < N ...
- Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级。
升级前 => 升级后 Spring Boot 1.5.x => Spring Boot 2.0.2 Spring Cloud Edgware SR4 => Spring Cloud ...
- NeuHub图像垃圾分类api和百度图像识别api
京东 NeuHub图像垃圾分类申请:http://neuhub.jd.com/gwtest/init/242 文档:https://aidoc.jd.com/image/garbageClassifi ...
- docker的一些常用操作
镜像:一个打包好的应用,还有应用运行的系统.资源.配置等容器:镜像的实例,一个镜像可以有一个或多个实例(容器)对docker容器的变更时写到容器的文件系统的,而不是写到docker镜像中的,可以用一个 ...
- [LeetCode] 491. Increasing Subsequences 递增子序列
Given an integer array, your task is to find all the different possible increasing subsequences of t ...