Created
January 23, 2017 02:16
-
-
Save 0532/a3972d4ef1f94701c87e54a8eb838e68 to your computer and use it in GitHub Desktop.
多线程任务处理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created By WangLichao On 2016年12月5日. | |
* 钱包多线程自动审单 | |
*/ | |
@Service | |
public class CheckOrderThreadTask { | |
private static final Logger bfLoanlog = LoggerFactory.getLogger("beforeLoan");//贷前 | |
@Resource(name="walletAutoCheckOrderService") | |
private WalletAutoCheckOrderService walletAutoCheckOrder; | |
// @Resource | |
// private ThreadCheckProcess threadCheckProcess; | |
public void execute(){ | |
//自动审核开关 | |
try { | |
String checkSwitch = ParameterPub.getSystemParam().getSystemSetting().getProperty("CHECK_SWITCH"); | |
String threadSwitch = ParameterPub.getSystemParam().getSystemSetting().getProperty("CHECK_THREAD_SWITCH"); | |
if("ON".equalsIgnoreCase(checkSwitch)){ | |
List<LoanOrder> list = walletAutoCheckOrder.autoCheckList(); | |
bfLoanlog.info("#####钱包自动审单定时器开始"); | |
if ("ON".equalsIgnoreCase(threadSwitch)) { | |
bfLoanlog.info("#####当前为多线程审核"); | |
if(list != null && list.size()>0){ | |
threadCheckProcess.setExecutor(3); | |
threadCheckProcess.threadCheckList(list,String.valueOf(Constants.PRODUCT_ID_STAGE)); | |
threadCheckProcess.clear(); | |
list = null; | |
System.gc(); | |
} | |
}else { | |
bfLoanlog.info("#####当前为单线程审核"); | |
walletAutoCheckOrder.autoCheckOrder(list); | |
} | |
} | |
bfLoanlog.info("钱包自动审单定时器开始 CheckOrderAuto==接口结束"); | |
} catch (Exception e) { | |
bfLoanlog.error("钱包自动审单定时器异常",e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created By WangLichao On 2016年12月7日. | |
*/ | |
public class ThreadCheckProcess { | |
private static final Logger beforeLoanlog = LoggerFactory.getLogger("beforeLoan");// 贷前 | |
private ExecutorService exec; // 线程池 | |
private int cpuNum; // 线程数 | |
private List<Future<Integer>> tasks = new ArrayList<Future<Integer>>(); // 接收线程处理的返回值 | |
/** | |
* 根据产品ID获取业务处理service | |
* | |
* @param productId | |
* @return | |
*/ | |
private BaseAutoCheck getService(String productId) { | |
BaseAutoCheck serivce = (BaseAutoCheck) SpringBeanUtil.getBean(productId + "check"); | |
return serivce; | |
} | |
public void threadCheckList(List<LoanOrder> checkOrderList,String productId) { | |
// 拆分任务,创建FutureTask并提交到Executor | |
ThreadCheckOrders checkOrdersThread = null; | |
FutureTask<Integer> task = null; | |
for (int i = 0; i < cpuNum; i++) { | |
int increment = checkOrderList.size() / cpuNum + 1; | |
int start = increment * i; | |
int end = increment * i + increment; | |
if (end > checkOrderList.size()) { | |
end = checkOrderList.size(); | |
} | |
checkOrdersThread = new ThreadCheckOrders(checkOrderList, productId, start, end); | |
task = new FutureTask<Integer>(checkOrdersThread); | |
tasks.add(task); | |
// Future<String> taskResult =(Future<String>) exec.submit(task); | |
exec.submit(task); | |
} | |
exec.shutdown();// 任务执行完毕,关闭线程池 | |
exec = null; | |
checkOrdersThread = null; | |
task = null; | |
} | |
class ThreadCheckOrders implements Callable<Integer> { | |
private List<LoanOrder> checkOrderList; | |
private int start; | |
private int end; | |
private String productId; | |
public ThreadCheckOrders(List<LoanOrder> checkOrderList,String productId, int start, int end) { | |
this.checkOrderList = checkOrderList; | |
this.start = start; | |
this.end = end; | |
this.productId = productId; | |
} | |
@Override | |
public Integer call() throws Exception { | |
int maxId = 0; | |
LoanOrder loanOrder = null; | |
List<LoanOrder> dataList = new ArrayList<LoanOrder>(); | |
for (int i = start; i < end; i++) { | |
loanOrder = (LoanOrder) checkOrderList.get(i); | |
dataList.add(loanOrder); | |
} | |
beforeLoanlog.info(productId+"-自动审核当前线程" + Thread.currentThread().getName() + "开始审单,审单数=" + dataList.size()); | |
if (dataList.isEmpty()) { | |
beforeLoanlog.info(productId+Thread.currentThread().getName()+"-dataList为空返回-1"); | |
return -1; | |
} | |
try { | |
BaseAutoCheck procesService = getService(productId); | |
procesService.autoCheckOrder(dataList); | |
maxId++; | |
} catch (Exception e) { | |
beforeLoanlog.error("autoCheckOrder失败" + e.getMessage()); | |
} | |
beforeLoanlog.info(productId+"-审单线程" + Thread.currentThread().getName() + "审单结束"); | |
return maxId; | |
} | |
} | |
public void clear() { | |
try { | |
exec = null; | |
tasks = null; | |
System.gc(); | |
} catch (Exception e) { | |
beforeLoanlog.error("自动审单定时器clear异常", e); | |
} | |
} | |
public void setExecutor(int cupNum) { | |
this.cpuNum = cupNum; | |
this.exec = Executors.newFixedThreadPool(cupNum); | |
this.tasks = new ArrayList<Future<Integer>>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment