关闭

并发编程实例---批量插入数据

标签: 并发
307人阅读 评论(0) 收藏 举报
分类:
public class SaveCouponNoTask implements Runnable {
  private Logger logger = LogManager.getLogger(SaveCouponNoTask.class);
  private ICouponTemplateBiz couponTemplateBiz;
  private CountDownLatch latch;
  private String sql;
  private List<String> params; // 本线程处理的参数列表

  public SaveCouponNoTask(CountDownLatch latch, String sql, List<String> params) {
    this.couponTemplateBiz = SpringContextHolder.getBean("couponBiz");
    this.latch = latch;
    this.sql = sql;
    this.params = params;
  }

  @Override
  public void run() {
    try {
      int each = 10000;
      int times = Double.valueOf(Math.floor(params.size() / each)).intValue();
      int totalCount = 0;
      for (int i = 0; i < times; i++) {
        int beforeCount = totalCount;
        totalCount += each;
        System.out.println("线程开始执行,ID:" + Thread.currentThread().getId() + ",当前时间:" + System.currentTimeMillis());
        couponTemplateBiz.batchInsertCoupon(sql, params.subList(beforeCount, totalCount));
        System.out.println("线程ID:" + Thread.currentThread().getId() + ",当前时间:" + System.currentTimeMillis());
      }
      if (totalCount < params.size()) {
        couponTemplateBiz.batchInsertCoupon(sql, params.subList(totalCount, params.size()));
      }
    } catch (Exception e) {
      logger.error("生成券插入数据库异常", e);
    } finally {
      latch.countDown();
    }
  }
}


private int generateCouponByActivityId(PromotionActivityListVo promotionActivityListVo, final int nums) {
    try {
      String sql = generateSimpleInsertCouponSql(promotionActivityListVo);
      List<String> params = new ArrayList<String>();
      List<String> couponNos = couponTemplateDao.getCouponNoByPromotionId(promotionActivityListVo.getId());
      Set<String> couponVoSet = new HashSet<String>(couponNos.size());
      for (String counponNo : couponNos) {
        couponVoSet.add(counponNo);
      }
      int i = 0;
      while (i < nums) {
        String couponNo = promotionActivityListVo.getCouponCode() + com.szy.utils.RandomStringUtils.getRandomString(4);
        boolean compareFlag = couponVoSet.contains(couponNo);
        if (compareFlag == false) {
          i++;
          params.add(couponNo);
          couponVoSet.add(couponNo);
        }
      }
      if (params.size() > 0) {
        int[] indexs = getIndexForSubList(params.size(), 1000, 10);
        CountDownLatch latch = new CountDownLatch(indexs.length - 1);
        for (int j = 1; j < indexs.length; j++) {
          taskExecutor
              .execute(new SaveCouponNoTask(latch, sql, params.subList(indexs[j - 1], indexs[j])));
        }
        new StatCouponNoSaveTask(latch, System.currentTimeMillis()).doTask();
        return params.size();
      }
    } catch (Exception e) {
      log.error(e);
      return 0;
    }
    return 0;
  }

	<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
		<!-- 核心线程数 -->	 
		<property name="corePoolSize" value="1" />
		<!-- 最大线程数 -->  
		<property name="maxPoolSize" value="2" />
		<!-- 队列最大长度 -->
		<property name="queueCapacity" value="10" />
		<!-- 线程池维护线程所允许的空闲时间,默认为60s -->
		<property name="keepAliveSeconds" value="10" />
	</bean>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:288096次
    • 积分:3669
    • 等级:
    • 排名:第8834名
    • 原创:86篇
    • 转载:80篇
    • 译文:0篇
    • 评论:40条
    最新评论
    FLASH学习
    一些官网