设置线程池参数,创建线程池
- corePoolSize要保留在池中的线程数,即使它们是空闲的,除非{@code - allowCoreThreadTimeOut}被设置
- maximumPoolSize允许在池中的最大线程数
- keepAliveTime当线程数大于核心时,这是多余的空闲线程将在终止前等待新任务的最大时间
- unit {@code keepAliveTime}参数的时间单位
- workQueue用于在执行之前保存任务的队列。这个队列将只保存由{@code execute}方法提交的{@code Runnable}任务
- threadFactory是执行器创建新线程时使用的工厂
- handler是执行被阻塞时使用的处理程序,因为线程边界和队列容量达到了
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
java
private static int corePoolSize = 50;//初始大小
private static int maximumPoolSize = 200; //最大值
//使用线程池管理
private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("RiskSafeguard-pool-%d").build();
private static ExecutorService pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize,
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
excel导入
java
/**
* 不动产抵押导入
* @param file
* @param createBy
* @return
*/
public boolean importData(MultipartFile file, Long createBy, Long itemApplyId){
// 创建内部类的实例对象
LoanItemRiskSafeguardService.ImportDataTask importDataTask = new LoanItemRiskSafeguardService.ImportDataTask(file, createBy, itemApplyId);
pool.execute(importDataTask);
return true;
}
LoanItemRiskSafeguardService业务类
java
/**
* 导入任务
* 实现Runable接口的内部类,重写run()
*/
public class ImportDataTask implements Runnable{
MultipartFile file = null;
Long createBy = null;
Long itemApplyId = null;
public ImportDataTask(MultipartFile file, Long createBy, Long itemApplyId){
this.file = file;
this.createBy = createBy;
this.itemApplyId = itemApplyId;
}
@Override
public void run() {
doImportDataTask(file, createBy, itemApplyId);
}
}
/**
* excel字段匹配
* @param file
* @param createBy
*/
public void doImportDataTask(MultipartFile file, Long createBy, Long itemApplyId){
/**
* excel数据的keys
*/
try{
String[] keys = new String[]{ // 读取excel后,每个列对应一个key
"riskSafeguardName", // 登记权属人
// ....
};
// 导入数据
List<Map<String, Object>> excelData = ImportUtil.importExcel(file, keys, 0); // 读取excel数据
if (!excelData.isEmpty()) {
Map<String, java.lang.Object> excelMap = null;
int success = 0,failed = 0;
for(int i=0;i<excelData.size();i++) {
excelMap = excelData.get(i);
// 设置担保措施对象值,插入数据库 --- start
LoanItemRiskSafeguard riskSafeguard = new LoanItemRiskSafeguard();
// 从excelMap中根据key获取到value,设置到对象中,后续存表
// 登记权属人
String riskSafeguardName = excelMap.get("riskSafeguardName")==null?"":excelMap.get("riskSafeguardName").toString();
riskSafeguard.setRiskSafeguardName(riskSafeguardName);
// ...
try {
if(failed<=i){
dao.insert("add",riskSafeguard);
}
}catch (Exception e){
failed=i+1;
e.printStackTrace();
}
if(failed <= i){
success = i + 1;
}else{
Long riskSafeguardId = riskSafeguard.getRiskSafeguardId();
dao.delById(riskSafeguardId);
}
// 设置担保措施对象值,插入数据库 --- end
}
}
} catch (BizException be) {
logger.info("后台导入不动产抵押物excel失败", be);
} catch (Exception e) {
logger.error("后台导入不动产抵押物excel失败", e);
}
}