list分批异步处理

    <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;
	}
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值