前言
项目有许多统计的功能,有些统计页面,要展示几个统计的结果,用户通过前台设置相关参数,后台实时统计并返回数据。后台查询正常查询结果是串行的。
最好的用户体验,就是每一个操作都可以实时的展示数据,3秒之内应该是用户的忍受范围之内的了,所以做一款产品不仅要考虑用户交互设计,后端的优化也是比不可少的。
举一个物流项目例子:
1:统计订单量
2:统计物流信息的时效
3:统计客户下单量(按高到低排序)
大家可以简单的看下以上这3项统计数据,总体来说,统计量还是不少的。最主要的还是要实时、实时、实时(重要的事情说三遍),显然定时任务是不现实的。
优化前:
程序的逻辑
优化后:
程序的逻辑
并行常见如:发邮件通知,短信通知等。
这里我们把并行的场景应用在统计这里。
多任务并行处理,适用于多核CPU,单核CPU多线程执行任务可能会适得其反(上下文切换以及线程的创建和销毁都会消耗资源),特别是CPU密集型的任务。
一:多任务并行处理
代码实现:
这里以demo实现,只需要把示例代码替换成业务代码即可
public class CountDownLatch Test {
final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
final static String beginTime = simpleDateFormat.format(new Date());
public static void main(String[] args) throws Exception{
CountDownLatch latch = new CountDownLatch(3);//
Gaci gaci1 = new Gaci("任务1", 1000, latch);
Gaci gaci2 = new Gaci("任务2", 2000, latch);
Gaci gaci3 = new Gaci("任务3", 2000, latch);
gaci1.start();// 任务1开始执行
gaci2.start();// 任务2开始执行
gaci3.start();// 任务3开始执行
latch.await();// 等待所有任务结束
System.out.println("所有的统计任务执行完成:" + simpleDateFormat.format(new Date()));
}
static class Gaci extends Thread {
String gaciName;// 名称
int runTime;// 模拟业务代码运行时间
CountDownLatch latch;// latch
public Gaci(String gaciName, int runTime, CountDownLatch latch) {
this.gaciName = gaciName;
this.runTime = runTime;
this.latch = latch;
}
public void run() {