需求背景:世界杯赛况开奖时候,将所有用户投注记录状态修改中奖或为中奖,
解决方法:创建线程池,线程去批量处理投注状态,线程池的任务队列是一个Runnable集合,工作线程负责从任务队列中取出并执行Runnable对象
//创建一个线程池
private ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*2);
//派奖,schedule,是我自己的业务对象,不需要管
public String batchExcute(Schedule schedule){
Map<String, Object> params = new HashMap<String, Object>();
int succeed = 0;
int fail = 0;
int total = 0;
params.put("scheduleId",schedule.getId());
params.put("status",0);
List<BettingRecordModel> list = bettingRecordMapper.list(params);
//mybaties查询出来来的所有待执行集合
if( list.size() >0 ){
//将list 拆分 list<list> ,小的list每个都是200条,如1000条记录,就有5个单独list,5个单独list又放到list中
List<List<BettingRecordModel>> list_list = averageAssign(list,200);
List<Future<Map<String,Integer>>> resultList = new ArrayList<>();
OpenPrizeThread thread=null;
for (List<BettingRecordModel> temp : list_list){
//创建一个线程任务,一个线程任务执行200条
thread = new OpenPrizeThread();
thread.setList(temp);//参数放入线程中
thread.setSchedule(schedule);//参数放入线程中
//启动线程并返回执行结果
Future<Map<String,Integer>> future = executorService.submit(thread);
resultList.add(future);
}
//executorService.shutdown();不建议shutdown,shutdown之后需要重新开启线程池,不然会报错
Map<String,Integer> result;
for (Future<Map<String,Integer>> fs : resultList) {
try {
result = fs.get();
total += result.get("total");
succeed += result.get("succeed");
fail += result.get("fail");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
executorService.shutdownNow();
e.printStackTrace();
}
}
}
return "执行总次数"+total+",成功"+succeed+"次,失败"+fail+"次";}
//将list 拆分List<list<>>,
public List<List<BettingRecordModel>> averageAssign(List<BettingRecordModel> source,int n){
List<List<BettingRecordModel>> result=new ArrayList<>();
if( source.size()<n){
result.add(source);
}else{
int remaider=source.size()%n; //(先计算出余数)
int number=source.size()/n; //然后是商
for(int i=0;i<number;i++){
List<BettingRecordModel> value=null;
if( remaider>0 ){
value=source.subList(source.size()-remaider,source.size());
remaider=0;
i--;
}else{
value=source.subList(i*n, (i+1)*n);
}
result.add(value);
}
}
return result;
}
//下面这个类处理自己相关的业务,大量的代码都是我自己的业务,大家实现Callable<Map<String,Integer>>接口就可以了
package com.rongdu.cashloan.core.service.impl;
import com.rongdu.cashloan.core.domain.Schedule;
import com.rongdu.cashloan.core.enums.WorldCupConfigEnum;
import com.rongdu.cashloan.core.model.BettingRecordModel;
import com.rongdu.cashloan.core.service.BettingRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tool.util.BeanUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/**奖金派发线程类
* Created by 15935 on 2018/5/5.
*/
public class OpenPrizeThread implements Callable<Map<String,Integer>>{
private static final Logger logger = LoggerFactory.getLogger(OpenPrizeThread.class);
private List<BettingRecordModel> list;
private Schedule schedule;
@Override
public Map<String,Integer> call() throws Exception {
Integer succeed = 0;
Integer fail = 0;
Integer total = 0;
double rebate_money = 0.0;//派奖金额
Map<String,Integer> result = new HashMap<>();
//实例化service
BettingRecordService bettingRecordService = (BettingRecordService) BeanUtil.getBean("bettingRecordService");
for( BettingRecordModel record : list) {
//如果是未开奖
if (record.getResult().equals(0)) {
try {
succeed++;
total++;
}catch (Exception e){
fail++;
total++;
}
}
}
result.put("total",total);
result.put("succeed",succeed);
result.put("fail",fail);
return result;
}
public List<BettingRecordModel> getList() {
return list;
}
public void setList(List<BettingRecordModel> list) {
this.list = list;
}
public Schedule getSchedule() {
return schedule;
}
public void setSchedule(Schedule schedule) {
this.schedule = schedule;
}
}