java线程池例子,多线程
场景描述:
在工作中一个秒杀的业务场景中,在后台系统秒杀活动管理中有一个名单录入功能,需要录入一个excel表格,当excel表格中数据达到三万时出现了sql的异常 ORA-01745;
批量插入数据的DAO代码:
int insertBatch(List<FsoActivityUserList> itemList);
批量插入数据的xml代码:
<insert id="insertBatch" parameterType="java.util.List">
insert into FSO_ACTIVITY_USER_LIST (ID, PRODUCT_CODE, U_ID, IS_WHITE_BLACK)
select <include refid="TABLE_SEQUENCE" />,A.* from(
<foreach collection="list" item="item" index="index" separator="union all" >
select
#{item.productCode},#{item.uId},#{item.isWhiteBlack}
from dual
</foreach>
) A
</insert>
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import cn.creditease.zeus.server.model.normandy.fso.FsoActivityUserList;
import cn.creditease.zeus.server.service.normandy.ActivityUserListService;
/**
* 鐢ㄤ簬娣诲姞涓囨潯浠ヤ笂鐨別xcel鏁版嵁
* @author charlotte
*
*/
public class ActivityExcelDataSaveRunnable implements Runnable {
private static final Logger log = Logger
.getLogger(ActivityController.class);
private ActivityUserListService activityUserListService;
private List<FsoActivityUserList> excelDataList = new ArrayList<FsoActivityUserList>();//excel瑙f瀽鍚庣殑鏁版嵁闆嗗悎
public ActivityExcelDataSaveRunnable(ActivityUserListService activityUserListService, List<FsoActivityUserList> excelDataList) {
this.activityUserListService = activityUserListService ;
this.excelDataList = excelDataList;
}
public void run() {
try {
Map<String, Object> addUserListBatchResult = activityUserListService.addUserListBatch(excelDataList);//鎵归噺鎻掑叆娲诲姩鍚嶅崟鏁版嵁
if ("1".equals(addUserListBatchResult.get("code"))) {//寮傚父澶勭悊
log.info("ActivityController#addActivity()----[zeus erro]...");
throw new Exception("zeus鎻掑叆鏁版嵁澶辫触,璇锋鏌eus鏃ュ織!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用:
private static ExecutorService pool=Executors.newCachedThreadPool();
private ActivityUserListService activityUserListService;
public staitc void main(String[] args){
List<FsoActivityUserList> saveList = new ArrayList<FsoActivityUserList>();
ActivityExcelDataSaveRunnable run = new ActivityExcelDataSaveRunnable(activityUserListService,saveList);
pool.execute(run);
}