多线程分段处理List集合
最近遇到问题:前端传来大量数据三层嵌套集合,楼主对封装数据花费了很长时间比较浪费时间,用MyBatisPlus处理批量操作的时候显得很慢,所以楼主在xml文件中进行了< foreach >操作,还是慢,所以就对集合进行了线程处理。
数据模型(当然不是两条啦):
[
{
"cableWellNo": "xx",
"cableWellName": "test-xx1号",
"baseorgId": "xx网格",
"addr": "xx省xx市xx区",
"longitude": "120.619585",
"latitude": "31.299379",
"coverType": "方形",
"coverLocation": "xx2号单元楼旁边",
"topBuriedDepth": 8,
"cmaterialType": "水泥",
"sectionNum":3,
"holeNum":7,
"longth": 10,
"wideth": 5,
"deepth": 10,
"remarks": "无",
"cableSectionList": [
{
"orientationType": "东",
"holeNum":3,
"cableHoleList": [
{
"cableHoleNo": "xx",
"cableHoleName": "xx01"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx02"
}
]
},
{
"orientationType": "南",
"holeNum":2,
"cableHoleList": [
{
"cableHoleNo": "xx",
"cableHoleName": "xx05"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx06"
}
]
},
{
"orientationType": "西",
"holeNum":3,
"cableHoleList": [
{
"cableHoleNo": "xx",
"cableHoleName": "xx07"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xxx08"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx09"
}
]
}
]
},
{
"cableWellNo": "xx",
"cableWellName": "test-xx2号",
"baseorgId": "xx网格",
"addr": "xx省xx市xx区",
"coverType": "圆形",
"coverLocation": "xx2号单元楼旁边",
"longitude": "120.619585",
"latitude": "31.299379",
"topBuriedDepth": 15,
"cmaterialType": "铸钢",
"longth": 10,
"sectionNum":9,
"holeNum":7,
"wideth": 3,
"deepth": 15,
"remarks": "无",
"cableSectionList": [
{
"orientationType": "东",
"holeNum":4,
"cableHoleList": [
{
"cableHoleNo": "xx",
"cableHoleName": "xx01"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx02"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx03"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx04"
}
]
},
{
"orientationType": "北",
"holeNum":2,
"cableHoleList": [
{
"cableHoleNo": "xx",
"cableHoleName": "xx05"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx06"
}
]
},
{
"orientationType": "西",
"holeNum":3,
"cableHoleList": [
{
"cableHoleNo": "xx",
"cableHoleName": "xx07"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx08"
},
{
"cableHoleNo": "xx",
"cableHoleName": "xx09"
}
]
}
]
}
]
public static void main(String[] args) {
// 开始时间
long start = System.currentTimeMillis();
List<String> list = new ArrayList<String>();
//模拟数据
for (int i = 1; i <= 3000; i++) {
list.add(i + "");
}
// 每50条数据开启一条线程
int threadSize = 50;
// 总数据条数
int dataSize = list.size();
// 线程数
int threadNum = dataSize / threadSize + 1;
// 定义标记,过滤threadNum为整数
boolean special = dataSize % threadSize == 0;
// 创建一个线程池
//动态创建线程池newFixedThreadPool
ExecutorService exec = Executors.newFixedThreadPool(threadNum);
// 定义一个任务集合
List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
Callable<Integer> task = null;
List<String> cutList = null;
// 确定每条线程的数据
// List集合分段
for (int i = 0; i < threadNum; i++) {
if (i == threadNum - 1) {
if (special) {
break;
}
cutList = list.subList(threadSize * i, dataSize);
} else {
cutList = list.subList(threadSize * i, threadSize * (i + 1));
}
//System.out.println("第" + (i + 1) + "组:" + cutList.toString());
final List<String> listStr = cutList;
/* 匿名内部类Java8写法 看个人习惯
task = () ->{
System.out.println(Thread.currentThread().getName() + "线程:" + listStr);
return 1;
};*/
task = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println(Thread.currentThread().getName() + "线程:" + listStr);
return 1;
}
};
// 这里提交的任务容器列表和返回的Future列表存在顺序对应的关系
tasks.add(task);
}
List<Future<Integer>> results = null;
try {
results = exec.invokeAll(tasks);
for (Future<Integer> future : results) {
System.out.println(+future.get());
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
// 关闭线程池
exec.shutdown();
System.err.println("线程任务执行结束");
System.err.println("执行任务消耗了 :" + (System.currentTimeMillis() - start) + "毫秒");