通过多线程处理集合里面的大量数据,提高数据导出效率。

前言:前段时间在写表格导出的时候,需要导出的数据集合需要循环处理,但是速度太慢,所以看了一下如何使用多线程加快导出速度,如有不足之处,请多批评指正。

注意:对象类实现Cloneable接口,不然导出会发现数据混乱。

@Data
public class EmployeeException extends BaseObject implements Cloneable {
   
   
    /**
    * 异常员工ID
    */
    private String employeeId;

    /**
    * 异常日期
    */
    private Date exceptionDate;

    /**
    * 1.未签到异常 2.未签退异常 3.未提交任务异常 4.未审批任务异常 5.工时异常
    */
    private Integer exceptionType;

    /**
     * 用于模糊搜索
     */
    private String name;
    private String startTime;
    private String endTime;
    private String departmentId;
    private String projectId;
    /**
     * 用于展示详情
     */
    private String signInTime;
    private String signLocation;
    private String signOutTime;
    private String signOutLocation;
    private String projectName;
    private String startTimeForCut;
    private String endTimeForCut;

    /**
     * 新增status字段,用来判断此异常是否已经审批通过异常报备  异常报备的状态:0.未审批 1:审批通过 2:审批驳回
     */
    private Integer status;

    @Override
    public EmployeeException clone() throws CloneNotSupportedException {
   
   
        return (EmployeeException)super.clone();
    }

}

1、Controller层方法

public void exportEmployeeException(employeeException) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        InputStream inStream = null;
        OutputStream outputStream = null;
        String isExport = "succ";
        HttpSession session = this.getServletRequest().getSession();
        try {
            employeeExceptionService.exportEmployeeException2(employeeException, os);
            String fileName = URLEncoder.encode(dateFormat.format(new Date()) + "列表");
            os.flush();
            byte[] buf = os.toByteArray();
            log.info("生成excel长度:" + buf.length + "字节");
            inStream = new ByteArrayInputStream(buf);
            this.getServletResponse().reset();
            this.getServletResponse().setContentType("application/vnd.ms-excel");
            this.getServletResponse().setCharacterEncoding("UTF-8");
            this.getServletResponse().addHeader("Content-Disposition",
                    "attachment; filename=\"" + fileName + ".xlsx\"");
            byte[] b = new byte[1024];
            int len;
            outputStream = this.getServletResponse().getOutputStream();
            while ((len = inStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
        } catch (Exception e) {
            log.error("{}", e);
            isExport = "fail";
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (inStream != null) {
                    inStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (Exception e) {
                logger.error("os close exception!!", e);
            }
            session.setAttribute("isExport", isExport);
        }
    }

2、service层方法

注:handleList(employeeException, employeeExceptionList, 5)方法用到了多线程的知识。

public void exportEmployeeException2(EmployeeException employeeException, ByteArrayOutputStream os) throws InterruptedException {
        List<EmployeeException> employeeExceptionList = employeeExceptionMapper.selectAllEmployeeExceptionList(employeeException);
        handleList(employeeException, employeeExceptionList, 5);
        ArrayList<Map<String, String>> data = new ArrayList<>();
        for (EmployeeExceptionExport employeeExceptionExport : employeeExceptionExportList) {
            Map<St
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值