本文主要是介绍多线程 分批调用方法接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
工具类
public abstract class BatchHandlerList<T> implements BatchHandlerInterface<T> {private static final Logger LOGGER = Logger.getLogger(BatchHandlerList.class);//每次处理条数private Integer perNum;private List<T> aylist;public BatchHandlerList(Integer perNum, List<T> aylist) {super();this.perNum = perNum;this.aylist = aylist;}/*** 分批调用方法* */public void handlerList(){try{if(aylist!=null && aylist.size() > 0){int size = aylist.size();int startIndex = 0;int endIndex = 1;int num = 1;if (size > perNum) {num = size / perNum;}for (int i = 1; i <= num; i++) {endIndex = (i) * perNum > size ? size : (i) * perNum;List<T> subList = aylist.subList(startIndex, endIndex);startIndex = perNum * i;if (subList!=null && subList.size() > 0) {handler(subList);}if (num == i && perNum * num < size) {//最后一批处理subList = aylist.subList(perNum * num, size);if (subList.size() > 0) {handler(subList);}}}}}catch(Throwable e){LOGGER.error("batchHandlerList handler exception",e);//错误回调方法可以重写errorHandler();}}public void errorHandler(){};
}
实现方法
//组装数据BatchHandlerList<String> handler = new BatchHandlerList<String>(20,waybillNos) {@Overridepublic void handler(List<String> subList) {//这里的records用的是引用传递 后面要用SispInfoRequestInfoRunnable sispRunable = new SispInfoRequestInfoRunnable(subList, contextOrder, language, count, records);sispInfoExecutor.execute(sispRunable);runList.add(sispRunable);}};//执行调用handler.handlerList();
SispInfoRequestInfoRunnable
import java.util.List;
import java.util.Map;import org.apache.log4j.Logger;import com.sf.iec.common.util.SispWsUtil;
import com.sf.iec.operationbusiness.sisp.bean.BarRecord;public class SispInfoRequestInfoRunnable implements Runnable{protected final Logger LOGGER = Logger.getLogger(getClass());private List<String> subList;private String contextOrder;private String language;private Integer count;private Map<String, Map<String,BarRecord>> records;public SispInfoRequestInfoRunnable(List<String> subList,String contextOrder, String language, Integer count,Map<String, Map<String, BarRecord>> records) {super();this.subList = subList;this.contextOrder = contextOrder;this.language = language;this.count = count;this.records = records;}private boolean status = false; // 线程是否执行完成@Overridepublic void run() {try{Map<String, Map<String,BarRecord>> recordMap = SispWsUtil.querySispRecord(subList, contextOrder,language,count);records.putAll(recordMap);}catch(Throwable e){LOGGER.error("SispInfoRequestInfoRunnable running exception",e);}finally{status = true;}}public boolean getStatus() {return status;} public List<String> getSubList() {return subList;}public void setSubList(List<String> subList) {this.subList = subList;}public String getContextOrder() {return contextOrder;}public void setContextOrder(String contextOrder) {this.contextOrder = contextOrder;}public String getLanguage() {return language;}public void setLanguage(String language) {this.language = language;}public Integer getCount() {return count;}public void setCount(Integer count) {this.count = count;}public Map<String, Map<String, BarRecord>> getRecords() {return records;}public void setRecords(Map<String, Map<String, BarRecord>> records) {this.records = records;}public Logger getLOGGER() {return LOGGER;}public void setStatus(boolean status) {this.status = status;}public SispInfoRequestInfoRunnable(){}
}
这篇关于多线程 分批调用方法接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!