🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 一、概述 比如轮询大量的记录,这种job,可能会一次轮询耗时数个小时到数天,那么,对于业务来说,是无法接受的;这个时候,平台支持通过引入派生多线程的并发解决方案; ## 二、解决方案 通过动态派生多线程来实现,通过一个例子,来参考实现; #### **调用核心逻辑** ``` SynCardDataApi api = getJobLogicTransactionalApi("synCardDataApi"); StringBuffer hql = new StringBuffer("from CardInfo where USER_ID >0"); List<CardInfo> cardlist = manager.getTransactionalCardDao().querySimpleHql(hql.toString()); List<List<CardInfo>> cardpackageList = ListUtil.splitListByPerListSize(cardlist, 1000); int threadNum = cardpackageList.size(); new SynCardDataRunCaller().call(threadNum, cardpackageList, api, manager, jobexecution); ``` #### **派生调用类** ``` public static class SynCardDataRunCaller extends TemporyTaskCaller { @Override public void createThreadToExecutionTask(int threadNum, ExecutorService executor, CountDownLatch currentMainThreadLatch, Object[] callerTranferdBusinessObjects) { for (int i = 0; i < threadNum; i++) { List<List<CardInfo>> cardpackageList = getBusinessObject(0); List<CardInfo> cardinfolistPer = cardpackageList.get(i); Runnable task = new SynCardDataRunThread(currentMainThreadLatch, appendToEndOfCallerTranferdBusinessObjects(cardinfolistPer)); executor.execute(task); } } } ``` #### **派生线程类** ``` public static class SynCardDataRunThread extends TemporyTaskThread { public SynCardDataRunThread(CountDownLatch callerCountDownLatch, Object[] callerTranferdBusinessObjects) { super(callerCountDownLatch, callerTranferdBusinessObjects); } @Override public void doBusinessInThread(Object[] callerTranferdBusinessObjects) { logger.error("{}开始运行", Thread.currentThread().getName()); SynCardDataApi api = getBusinessObject(1); JobManager manager = getBusinessObject(2); JobExecution jobexecution = getBusinessObject(3); jobexecution.append("{}开始运行", Thread.currentThread().getName()); List<CardInfo> cardlist = getBusinessObject(4); for (CardInfo cardinfo : cardlist) { try { api.doProcess(manager, cardinfo, jobexecution); } catch (Exception ex) { UniframeworkLogUtil.logJobException(ex); } } logger.error("{}完成运行", Thread.currentThread().getName()); } } ```