线程池:利用线程池优化查询速度

时间:2023-01-17 18:56:00


对于同一个页面的两个无关业务,我们可以通过线程池来优化。

这里写了两个测试类,比较
顺序执行和利用线程池的查询速度差距

@Test
public void test211(){
Long beginTime = new Date().getTime();
System.out.println(beginTime+"--------当前时间");
List<ActivityInfo> activity=activityService.listall("");
List<ActivityInfo> activity2=activityService.listall("");
Long endTime = new Date().getTime();
System.out.println(endTime+"--------结束时间");
System.out.println(new Date().getTime()-beginTime+"--------花费时间");
}
@Test
public void test212() throws InterruptedException {
Long beginTime = new Date().getTime();
System.out.println(beginTime+"--------当前时间");
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(()->{
List<ActivityInfo> activity=activityService.listall("");
});
executorService.submit(()->{
List<ActivityInfo> activity2=activityService.listall("");
});
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
Long endTime = new Date().getTime();
System.out.println(endTime+"--------结束时间");
System.out.println(new Date().getTime()-beginTime+"--------花费时间");

}

这里是自己写的小的查询,所以区别不大,数据量上来之后就会有明显的速度差距。