多线程查数据库实例

2022/8/6 2:22:43

本文主要是介绍多线程查数据库实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 List<List<JSONObject>> group_list = CutUtil.groupList(dataList, 10);//暂时改成10(原为1000)
                List<Callable<List<JSONObject>>> tasks = new ArrayList<Callable<List<JSONObject>>>(); // 添加任务
                for(List<JSONObject> info_list : group_list){
                    counter++;
//                    System.out.println("------------股东穿透开始执行第"+layerNum+"层,第"+counter+"组查询-------------------");
                    Callable<List<JSONObject>> query_ = new QueryThread(relatedMapper, info_list, layerNum);
                    tasks.add(query_);
                }

/ 定义固定长度的线程池  防止线程过多
ExecutorService execservice = Executors.newFixedThreadPool(15);
List<Future<List<JSONObject>>> futures = execservice.invokeAll(tasks);

// 处理线程返回结果
if (ValidateObject.hasValueInCollection(futures)) {
    for(Future<List<JSONObject>> future : futures) {
        dataList2.addAll(future.get());
    }
}

自定义线程类:
package com.seeyii.util;

import java.util.List;
import java.util.concurrent.Callable;
import com.seeyii.web.download.mapper.RelatedMapper;
import net.sf.json.JSONObject;

public class QueryThread implements Callable<List<JSONObject>> {

    private RelatedMapper relatedMapper;
    private List<JSONObject> info_list;
    private int layerNum;

    /**
     * @param relatedMapper
     * @param info_list
     * @param layerNum
     */
    public QueryThread(RelatedMapper relatedMapper, List<JSONObject> info_list, int layerNum) {
        this.relatedMapper = relatedMapper;
        this.info_list = info_list;
        this.layerNum = layerNum;
    }

    @Override
    public List<JSONObject> call() throws Exception {
        try {
            List<JSONObject> list = null;
            list = relatedMapper.queryShareholders(info_list,layerNum);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

 



这篇关于多线程查数据库实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程