ExecutorService pool = null
try {
int batchCount = 100
int size = list.size();
int index = size / batchCount;
int mod = size % batchCount;
long startTime = System.currentTimeMillis();
def importFlag = false
//创建线程池
pool = Executors.newFixedThreadPool(40);
List<Future> futureList = new ArrayList<Future>();
for (int m = 0; m < index; m++) {
def subList = list.subList(m * batchCount, m * batchCount + batchCount);
Future f1 = pool.submit(new ReadSalaryExcelService(importFlag,subList,size,version));
futureList.add(f1) //获得返回值
}
if (mod > 0){
def modList = list.subList(index * batchCount, size);
ReadSalaryExcelService thread = new ReadSalaryExcelService(importFlag,modList,size,version)
Future f1 = pool.submit(thread);
futureList.add(f1) //获得返回值
}
//关闭线程池
pool.shutdown()
// TODO 这个方法是统计所有线程全部执行完毕的时间
while(true){
if(pool.isTerminated()){
long end = System.currentTimeMillis();
System.out.println("40个线程执行用时: " + (end - startTime) + "ms,执行数据总量===="+size);
break;
}
}
} catch (Exception e) {
//关闭线程池
if (pool!=null){
pool.shutdown()
}
//操作失败
dataSourceBySqlService.excuteUpdate(" update [table] set version = -1 where version = ${version} ")
e.printStackTrace()
throw e
}
return ReturnT.SUCCESS;
}
class ReadSalaryExcelService implements Callable {
boolean importFlag
def list
int size
def version
public ReadSalaryExcelService(boolean importFlag,def list,int size,def version) {
this.importFlag = importFlag
this.list = list
this.size = size
this.version = version
}
@Override
public Object call() {
try {
//业务逻辑代码
batchBaseModel(list,size,version)
} catch (Exception e) {
log.error("========thirdCommunityPriceJob========" + e.message)
importFlag = true
}
return importFlag
}
}