<bean id="threadPool" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数 -->
<property name="corePoolSize" value="20" />
<!-- 最大线程数 -->
<property name="maxPoolSize" value="100" />
<!-- 队列最大长度 >=mainExecutor.maxSize -->
<property name="queueCapacity" value="60" />
<!-- 线程池维护线程所允许的空闲时间 -->
<property name="keepAliveSeconds" value="300" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略 -->
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>
</property>
</bean>
threadPool.execute(new Runnable() {
public void run() {
if (CollectionUtils.isNotEmpty(countyInfoVOList)) {
//分批处理
DataBatchUtils batchUtils = new DataBatchUtils(countyInfoVOList,200);
while (batchUtils.hasNext()){
List<CountyInfoVO> list = batchUtils.next();
业务处理逻辑
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
import com.google.common.collect.Iterables;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* Created with IntelliJ IDEA
*/
public class DataBatchUtils<E> implements Iterator<List<E>> {
private int batchSize;
private List<E> origin;
private int index = 0;
private List<E> result;
private int size = 0;
public DataBatchUtils(List<E> origin, int batchSize) {
if (0 >= batchSize) {
throw new RuntimeException();
}
this.batchSize = batchSize;
this.origin = origin;
this.size = null == origin ? 0 : origin.size();
result = new ArrayList<E>(batchSize);
}
@Override
public boolean hasNext() {
return index < size;
}
@Override
public List<E> next() {
result.clear();
for (int i = 0; i < batchSize && index < size; i++) {
result.add(origin.get(index++));
}
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
/**
* 批量插入ByGuava
*
* @param origin 需要批量操作的数据信任List
* @param batchSize 分批数
* @param <T>
* @return
*/
public static <T> List<List<T>> batchInsert(List<T> origin, int batchSize) {
Iterable<List<T>> subSets = Iterables.partition(origin, batchSize);
Iterator<List<T>> partitions = subSets.iterator();
List<List<T>> list = new LinkedList<>();
while (partitions.hasNext()) {
List<T> sub = partitions.next();
list.add(sub);
}
return list;
}
}