多线程进行数据统计

前言

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

一、设置线程逻辑

@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一下
		}
	}
}

总结

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

你可能感兴趣的:(多线程)