基于Spring 的ThreadPoolTaskExecutor 线程池分段遍历读取集合list
代码如下所示:
1、定义线程池
<bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!--初始线程池大小--> <property name="corePoolSize" value="10"/> <!--最大线程池大小--> <property name="maxPoolSize" value="30"/> </bean>
2、主线程代码
@Autowired public ThreadPoolTaskExecutor threadPoolTaskExecutor; private void doReadList() throws InterruptedException, ExecutionException{ /**初始化集合**/ List<String> list = new ArrayList<String>(); for(int i=0;i<100;i++){ list.add("test--"+i); } /**接收集合各段的 执行的返回结果**/ List<Future<Boolean>> futureList = new ArrayList<Future<Boolean>>(); /**集合总条数**/ int size = list.size(); /**将集合切分的段数**/ int sunSum = 10; int listStart,listEnd; /***当总条数不足10条时 用总条数 当做线程切分值**/ if(sunSum > size){ sunSum = size; } /**定义子线程**/ SunCallable sunCallable ; /**将list 切分10份 多线程执行**/ for (int i = 0; i < sunSum; i++) { /***计算切割 开始和结束**/ listStart = size / sunSum * i ; listEnd = size / sunSum * ( i + 1 ); /**最后一段线程会 出现与其他线程不等的情况**/ if(i == sunSum - 1){ listEnd = size; } /**线程切断**/ List<String> sunList = list.subList(listStart,listEnd); /**子线程初始化**/ sunCallable = new SunCallable(i,sunList); /***多线程执行***/ futureList.add(taskExecutor.submit(sunCallable)); } /**对各个线程段结果进行解析**/ for(Future<Boolean> future : futureList){ if(null != future && future.get()){ System.err.println("成功"); }else{ System.err.println("失败"); } } }
3 子线程SunCallable代码:
package xxx.xxx.xxx.xx; import java.util.List; import java.util.concurrent.Callable; public class SunCallable implements Callable<Boolean> { /**当前是属于第几段线程**/ private int pageIndex; private List<String> list; public SunCallable(int pageIndex,List<String> list){ this.pageIndex = pageIndex; this.list = list; } @Override public Boolean call() throws Exception { System.err.println(String.format("pageIndex:%s size:%s",pageIndex,list.size())); Boolean result = Boolean.TRUE; if(null != list && list.size() >0){ for(String str: list){ try { //TODO 业务处理 } catch (Exception e) { result = Boolean.FALSE;; } } } return result; } }