private String openThreadImportData( List<ZentaoBug> dataList) {
int dataSize = dataList.size(); //初始化總數
int threadSize = 1000;//線程容量,每5000條數據開啓一條線程
int threadNum = dataSize / threadSize + 1;// 線程數
boolean special = dataSize % threadSize == 0;// 定義標記,過濾threadNum爲整數
// 創建一個固定可重用線程池
ExecutorService exec = Executors.newFixedThreadPool(threadNum);
// 線程個數 存貨
List<Future<Integer>> futures = new ArrayList<Future<Integer>>(threadNum);
// 根據線程容量,爲當前線程截取的數據
List<ZentaoBug> subList = Lists.newArrayList();
// 線程的個數
for (int i = 0; i < threadNum; i++) {
if (i == threadNum - 1) { // 最後一個線程
if (special) {
break;
}
// 截取第n*ThreadSize個容量位置--至最後
subList = dataList.subList(threadSize * i, dataSize);
} else {
// 截取的內容爲 第n個容量位置--至 線程容量*(i+1),
subList = dataList.subList(threadSize * i, threadSize * (i + 1));
}
final List<ZentaoBug> list = subList;
Callable<Integer> task = () -> {
List<ZentaoBug> batchList = Lists.newArrayList();
// do 2 需要排除的數據 和已存在的數據
List<ZentaoBug> isExistBugs = findisExistBug(list);
Set<String> existBugIDs = isExistBugs.stream().map(ZentaoBug::getBugID).collect(Collectors.toSet());
for (ZentaoBug zentaoBug : list) {
if (filterBug(zentaoBug)) {
continue;
}
//計算解決時間
computeZentaoBug(zentaoBug);
//添加到集合
batchList.add(zentaoBug);
}
// do 3 保存活更新
//批量保存
this.saveBatch(batchList);
return 1;
};
futures.add(exec.submit(task));// 添加線程
}
// 判斷線程是否結束 exec.isShutdown
exec.shutdown();// 關閉線程池
//getResult(dataSize, filtedNum.get(), existNum.get())
return "上傳成功";
}
FixedThreadPool線程池(記錄一下)executor
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.