多线程进行数据统计

前言

对数据量较大和子表较多的查询进行统计。

一、设置线程逻辑

@Service(value = "ticket_CheckStatisticService")
public class CheckStatisticService {
	@Resource
	private CommonStatisticService commonStatisticService;
    //创建线程池
	private static ExecutorService exec = Executors.newCachedThreadPool();
        //同一天分多个时间段进行查询
	public void batchInsert(Date date) {  
        
		long start = System.currentTimeMillis();
		String dateStr = DateUtil.formatDateToString(date, "yyyy-MM-dd") + " %s:%s:%s";
		
        int len = 8;  //分8个线程
		int step = 24 / len; // 每 3 小时进行一次查询 
        
		CountDownLatch countDownLatch = new CountDownLatch(len); //最大线程运行的时间,作为结束时间

		for (int i = 0; i < len; i++) {
			int startHour = i * step;
			int endHour = (i + 1) * step - 1;
			if (endHour >= 24) {
				endHour = 23;
			}
			String startHourStr = startHour < 10 ? "0" + startHour : "" + startHour;
			String endHourStr = startHour < 10 ? "0" + endHour : "" + endHour;
			exec.execute(new CheckStatisticThread(String.format(dateStr, startHourStr, "00", "00"),
					String.format(dateStr, endHourStr, "59", "59"), countDownLatch, commonStatisticService));
		}
		try {
			countDownLatch.await();
			System.out.println("插入执行时间:" + (System.currentTimeMillis() - start) + "ms");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

二、创建线程池

public class CheckStatisticThread implements Runnable {

	private CommonStatisticService commonStatisticService;

	private CountDownLatch countDownLatch;

	private String startTime;

	private String endTime;
    //设置每个线程的初始化,以及传参
	public CheckStatisticThread(String startDate, String endDate, CountDownLatch countDownLatch,
			CommonStatisticService commonStatisticService) {
		super();
		this.startTime = startDate;
		this.endTime = endDate;
		this.countDownLatch = countDownLatch;
		this.commonStatisticService = commonStatisticService;
	}

	@Override
	public void run() {
		try {
		    //执行操作 do.................
		} finally {
			countDownLatch.countDown();  //每个线程执行结束后down一下
		}
	}
}

总结

在做数据量比较大的情况,并且查询的数据子表比较多的情况下,我们可以使用多线程进行插入到一张空表里,然后执行单表操作,这样性能可以提高不少。。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值