开启多个线程查询大量数据

最近有个业务需求要批量导出大量的数据,但数据库在面对过多数据查询时效率就会很慢,于是考虑使用多个线程来实现。

这里使用了Callable的方式创建多线程,好处是有返回值

package com.next.jiangzh.film.dao;
 
import java.util.List;
import java.util.concurrent.Callable;
import com.next.jiangzh.film.dao.mapper.NextUserMapper;
 

public class test implements Callable<List> {
 
    private int start;//当前页数
 
    private int num;//每页查询多少条
 
    private List page;//每次分页查出来的数据
 
    public  test(int start,int num,NextUserMapper nextUserMapper) {
        this.start=start;
        this.num=num;
        //分页查询数据库数据
        page=nextUserMapper.getUsers(start, num);
    }
 
    @Override
    public List call() throws Exception {
        //返回数据给Future
        return page;
    }
}

测试方法

这里使用ExecutorService 方法创建线程,因为我的表里数据量较少,因此设置每次查询3条数据。

package com.next.jiangzh.film.dao.mapper;

import com.next.jiangzh.film.controller.cinema.vo.CinemaFilmInfoVO;
import com.next.jiangzh.film.controller.cinema.vo.CinemaFilmVO;
import com.next.jiangzh.film.controller.cinema.vo.FieldHallInfoVO;
import com.next.jiangzh.film.dao.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FilmFieldTMapperTest2 {

    @Autowired
    private NextUserMapper nextUserMapper;

    @Test
    public void describeFieldListTest() throws InterruptedException, ExecutionException{
    	 List<List> result = new ArrayList<>();//返回结果
         //查询数据库总数量
         int count = nextUserMapper.count();
         int num = 3;//一次查询多少条
         //需要查询的次数
         int times = count / num;
         if (count % num != 0) {
             times = times + 1;
         }
         //开始页数  连接的是Mysql的数据库  封装的分页方式  我的是从0开始
         int start = 0;
       //定义固定长度的线程池  防止线程过多
         ExecutorService executorService = Executors.newFixedThreadPool(15);
         //Callable用于产生结果
         List<Future<List>> futureList = new ArrayList<Future<List>>();
         for (int i = 0; i < times; i++) {
             Future<List> qfe = executorService.submit(new test(start, num,nextUserMapper));
             futureList.add(qfe);
             start += num;
         }
         
         //处理线程返回结果
         if(futureList!=null&&futureList.size()>0){
             for (Future<List> future:futureList){
            	 result.addAll(future.get());
             }
         }
  
         executorService.shutdown();//关闭线程池
    }

}

最后看结果:
在这里插入图片描述由此可见数据的确是查询到了,至于性能和之前相比怎么样以后多插入点数据再试。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值