java并发查询数据

并发服务:通过继承一个Callable接口来回去并发后,个线程的返回结果,线程中调用的方法返回什么结果,这里<>中的泛型就指定什么类型,也可以直接Object,注意基础的方法中的call方法返回类型和基础的Callable接口中的类型保持一致

package com.springboot_android.thread;

import com.project_entity.bean.DeviceRecrodBean;
import com.springboot_android.dao.DeviceRecrodDao;

import java.util.*;
import java.util.concurrent.Callable;

/**
 * 历史数据,多线程处理
 */
public class ThreadCall implements Callable<Map<String,Object>>{

    private String identifier;

    private String deviceName;

    private String productKey;

    private Date startTime;

    private Date endTime;

    private DeviceRecrodDao deviceRecrodDao;

    private int groupId;

    public ThreadCall(String identifier,String deviceName,String productKey,Date startTime,Date endTime,DeviceRecrodDao deviceRecrodDao,int groupId){
        this.identifier=identifier;
        this.deviceName=deviceName;
        this.productKey=productKey;
        this.startTime=startTime;
        this.endTime=endTime;
        this.deviceRecrodDao=deviceRecrodDao;
        this.groupId=groupId;
    }
//要执行的任务
    @Override
    public Map<String,Object> call() throws Exception {
//开始执行并发任务
        Map<String,Object> map=new HashMap<>();
        /*List<DeviceRecrodBean> list = deviceRecrodDao.selectDeviceProductKeyId( productKey, deviceName, identifier,startTime, endTime);
        map.put("value",list);
        map.put("identifier",identifier);
        map.put("productKey",productKey);
        map.put("deviceName",deviceName);
        map.put("groupId",groupId);*/
        return map;
    }

}

并发服务调用:
package com.springboot_android.thread;

import com.project_entity.bean.mobe.DeviceGroupGraphBean;
import com.springboot_android.dao.DeviceRecrodDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.*;

@Component
public class InfoMore {
    @Autowired
    private DeviceRecrodDao recrodDao;
    //根据集合中的内容查询所有数据
    public List<Object> moreInfoService(Date stateTime,Date endTime,List<DeviceGroupGraphBean> projectDataAccessBeanList) throws Exception {
        Map<Integer,DeviceGroupGraphBean> map4=new HashMap<>();
        // 创建一个线程池,我这边由于一次list集合的长度最多是5,所以按照集合数量创建线程,如果没法确定开启的线程数,建议这里指定一下线程数
        ExecutorService executorService = Executors.newFixedThreadPool(projectDataAccessBeanList.size());
        // 创建多个有返回值的任务
        List<Future> list = new ArrayList<Future>();
        for (DeviceGroupGraphBean dggb:projectDataAccessBeanList) {
            Callable callable = new ThreadCall(dggb.getIdentifier(),dggb.getDeviceName(),dggb.getProductKey(),stateTime,endTime,recrodDao,dggb.getGroupId());
            // 执行任务并获取Future对象
            Future future = executorService.submit(callable);
            list.add(future);
            map4.put(dggb.getGroupId(),dggb);
        }
        // 关闭线程池
        executorService.shutdown();
        // 获取所有并发任务的运行结果
        List<Map<String,Object>> list1=new ArrayList<>();
        for (Future future : list) {
            Map<String,Object> map=(Map<String,Object>) future.get();
            list1.add(map);
        }
        //分组信息
        List<Object> list2=new ArrayList<>();
        for (Integer j:map4.keySet()){
            Map<String,Object> map3=new HashMap<>();
            map3.put("groupId",j);
            map3.put("groupName",map4.get(j).getGroupName());
            List<Map<String,Object>> list3=new ArrayList<>();
            for (Map<String,Object> fututeMap:list1){
                if (map4.get(j).getGroupId()==fututeMap.get("groupId")){
                    Map<String,Object> map2=new HashMap<>();
                    map2.put("value",fututeMap.get("value"));
                    map2.put("identifier",fututeMap.get("identifier"));
                    map2.put("productKey",fututeMap.get("productKey"));
                    map2.put("deviceName",fututeMap.get("deviceName"));
                    list3.add(map2);
                }
            }
            map3.put("data",list3);
            list2.add(map3);
        }
        return list2;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值