springBoot中service层查询使用多线程CompletableFuture(有返回值)

重点:

1. 创建线程池

 Executor executor = Executors.newCachedThreadPool();
 //保存线程
   List<CompletableFuture<Void>> futures = new ArrayList<>();

2.使用

//这里可以是多个看下面代码是在for中使用的
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {},executor );

3.等待所有线程执行完毕

  futures.add(future);

4.等待所有线程执行完毕

 CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

byTimelist方法:

在这个byTimelist方法示例中,我们使用CompletableFuture.supplyAsync()将每个vehicleStatisticsMapper.chuchelist(chuchelistVo)方法调用包装起来,并返回一个CompletableFuture,其中结果的类型为Long。
然后,我们在相应的位置获取异步任务的结果,使用get()方法阻塞当前线程,直到结果可用。
package com.ruoyi.system.service.vehiclestatistics;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.vo.ChuchelistVo;
import com.ruoyi.system.domain.vo.VehicleStatisticsVo;
import com.ruoyi.system.mapper.vehiclestatistics.VehicleStatisticsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Service
public class VehicleStatisticsServiceimpl implements VehicleStatisticsService {
    @Autowired
    VehicleStatisticsMapper vehicleStatisticsMapper;
    @Override
    public List<VehicleStatisticsVo> byTimelist(String startTime, String endTime) {
        List<VehicleStatisticsVo> vehicleStatisticsVos = vehicleStatisticsMapper.byTimelist(startTime, endTime);
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        /**
         * 使用线程池的工厂方法创建线程池(会自动设置线程的数量大小)
         */
        Executor executor = Executors.newCachedThreadPool();
        for (VehicleStatisticsVo vehicleStatisticsVo : vehicleStatisticsVos) {
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                ChuchelistVo chuchelistVo = new ChuchelistVo();
                chuchelistVo.setStartTime(startTime);
                chuchelistVo.setEndTime(endTime);
                chuchelistVo.setDeptid(Long.valueOf(vehicleStatisticsVo.getBmId()));
                chuchelistVo.setChsingletype("值班");

                CompletableFuture<Long> zhibanFuture = CompletableFuture.supplyAsync(() ->
                        (long) vehicleStatisticsMapper.chuchelist(chuchelistVo).size(),executor);

                chuchelistVo.setChsingletype("紧急");

                CompletableFuture<Long> jjChucheFuture = CompletableFuture.supplyAsync(() ->
                        (long) vehicleStatisticsMapper.chuchelist(chuchelistVo).size(),executor);

                chuchelistVo.setChsingletype("正常");

                CompletableFuture<Long> chuCheFuture = CompletableFuture.supplyAsync(() ->
                        (long) vehicleStatisticsMapper.chuchelist(chuchelistVo).size(),executor);

                ChuchelistVo chuchelistVo2 = new ChuchelistVo();
                chuchelistVo2.setStartTime(startTime);
                chuchelistVo2.setEndTime(endTime);
                chuchelistVo2.setLongAndShortparame("and cc.sempid=0");
                chuchelistVo2.setDeptid(Long.valueOf(vehicleStatisticsVo.getBmId()));

                CompletableFuture<Long> dtChucheFuture = CompletableFuture.supplyAsync(() ->
                        (long) vehicleStatisticsMapper.chuchelist2(chuchelistVo2).size(),executor);

                chuchelistVo2.setLongAndShortparame("and cc.sempid!=0");

                CompletableFuture<Long> ctChucheFuture = CompletableFuture.supplyAsync(() ->
                        (long) vehicleStatisticsMapper.chuchelist2(chuchelistVo2).size(),executor);

                try {
                    vehicleStatisticsVo.setZhiban(zhibanFuture.get());
                    vehicleStatisticsVo.setJjChuche(jjChucheFuture.get());
                    vehicleStatisticsVo.setChuChe(chuCheFuture.get());
                    vehicleStatisticsVo.setDtChuche(dtChucheFuture.get());
                    vehicleStatisticsVo.setCtChuche(ctChucheFuture.get());
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            },executor);

            futures.add(future);
        }

        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();

        return vehicleStatisticsVos;
    }

    @Override
    public AjaxResult bytypelist(ChuchelistVo chuchelistVo1) {
        Map<String, Object> ms = new HashMap<>();
        Map<String, Object> rs = new HashMap<>();
        Map<String, Object> rs2 = new HashMap<>();
        ChuchelistVo chuchelistVo = new ChuchelistVo();
        chuchelistVo.setStartTime(chuchelistVo1.getStartTime());
        chuchelistVo.setEndTime(chuchelistVo1.getEndTime());
        chuchelistVo.setChsingletype(chuchelistVo1.getChsingletype());
        for (Long deptid : chuchelistVo1.getDeptids()) {
            chuchelistVo.setDeptid(deptid);
            rs2.put("部门id", deptid);
            rs2.put("部门名称", vehicleStatisticsMapper.chuchelist(chuchelistVo).size() > 0 ? vehicleStatisticsMapper.chuchelist(chuchelistVo).get(0).getDeptname() : ("" + deptid));
            rs2.put("listsize", (long) vehicleStatisticsMapper.chuchelist(chuchelistVo).size());
            rs2.put("list",vehicleStatisticsMapper.chuchelist(chuchelistVo));
            rs.put(vehicleStatisticsMapper.chuchelist(chuchelistVo).size() > 0 ? vehicleStatisticsMapper.chuchelist(chuchelistVo).get(0).getDeptname() : ("" + deptid),rs2);
        }
        ms.put("result", rs);
        ms.put("startTime", chuchelistVo.getStartTime());
        ms.put("endTime", chuchelistVo.getEndTime());
        return AjaxResult.success(ms);
    }

    @Override
    public AjaxResult bylongandshortparamelist(ChuchelistVo chuchelistVo1) {
        Map<String, Object> ms = new HashMap<>();
        Map<String, Object> rs = new HashMap<>();
        Map<String, Object> rs2 = new HashMap<>();
        ChuchelistVo chuchelistVo = new ChuchelistVo();
        chuchelistVo.setStartTime(chuchelistVo1.getStartTime());
        chuchelistVo.setEndTime(chuchelistVo1.getEndTime());
        if (chuchelistVo1.getLongAndShortparame() != null) {
            if ("长途".equals(chuchelistVo1.getLongAndShortparame())) {

                chuchelistVo.setLongAndShortparame("and cc.sempid=0");
            }
            if ("短途".equals(chuchelistVo1.getLongAndShortparame())) {
                chuchelistVo.setLongAndShortparame("and cc.sempid!=0");
            }
        }

        for (Long deptid : chuchelistVo1.getDeptids()) {
            chuchelistVo.setDeptid(deptid);
            List<ChuchelistVo> chuchelistVos = vehicleStatisticsMapper.chuchelist2(chuchelistVo);
            rs2.put(chuchelistVos.size() > 0 ? chuchelistVos.get(0).getDeptname() : ("" + deptid), (long) chuchelistVos.size());
            rs2.put("部门id", deptid);
            rs2.put("部门名称", chuchelistVos.size() > 0 ? chuchelistVos.get(0).getDeptname() : ("" + deptid));
            rs2.put("listsize", chuchelistVos.size());
            rs2.put("list", chuchelistVos);
            rs.put(chuchelistVos.size() > 0 ? chuchelistVos.get(0).getDeptname() : ("" + deptid), rs2);
        }
        ms.put("result", rs);
        ms.put("startTime", chuchelistVo.getStartTime());
        ms.put("endTime", chuchelistVo.getEndTime());
        return AjaxResult.success(ms);
    }

}

你可能感兴趣的:(多线程,spring,boot,后端,多线程)