使用ForkJoinPool完成多线程配置

新建自己的bean类,在类中完成多线程方法书写
package com.wisest.monitor.service.app.homepage.impl;

import com.wisest.monitor.dao.ViewHourGasMapper;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.RecursiveTask;

/**
 * Created by cyk on 2018/5/30.
 * 小指标统计组别多线程实现类
 */
public class SmallIndicatorThread extends RecursiveTask> {

    private HashMap> groupStringMap;

    private Long loop;

    private String tableName;

    private String type;

    private String longOrgCode;

    public SmallIndicatorThread(HashMap> groupStringMap, Long loop, String tableName, String type, String longOrgCode,ViewHourGasMapper viewHourGasMapper) {
        this.groupStringMap = groupStringMap;
        this.loop = loop;
        this.tableName = tableName;
        this.type = type;
        this.longOrgCode = longOrgCode;
        this.viewHourGasMapper = viewHourGasMapper;
    }

    private ViewHourGasMapper viewHourGasMapper;

    @Override
    protected HashMap compute() {
        try {
            return insertDateGroupId(groupStringMap,loop,tableName,type,longOrgCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    //根据组别按小时为单位计算
    private HashMap insertDateGroupId(HashMap> groupStringMap, Long loop, String tableName, String type, String longOrgCode) throws SQLException {
        //用于存放回传数据
        HashMap resultMap = new HashMap<>();
        for (long x = 1; x <= loop; x++) {
            StringBuffer sql = new StringBuffer();
            //表名可更改
            sql.append("SELECT SUM(re.num) sumNum,COUNT(re.num) countNum FROM (");
            sql.append("SELECT SUM (CASE WHEN VALUE > 0.0 THEN VALUE ELSE 0.0 END ) as num FROM  ");
            sql.append(tableName);
            sql.append(" WHERE stand_name_c = '");
            sql.append(type);
            sql.append("' AND orgcode LIKE '");
            sql.append(longOrgCode);
            sql.append("%' GROUP BY timestamp HAVING (");
            int p = 0;
            List strings = groupStringMap.get(String.valueOf(x));
            for (String string : strings) {
                String[] split = string.split(",");
                if (p < 1) {
                    p++;
                } else {
                    sql.append("or");
                }
                sql.append("( timestamp >= '");
                sql.append(split[0]);
                sql.append("' AND timestamp < '");
                sql.append(split[1]);
                sql.append("')");

            }
            sql.append(") and SUM ( CASE WHEN VALUE > 0.0 THEN VALUE ELSE 0.0 END ) > 0) re");
            System.out.println(sql.toString());


            long a = System.currentTimeMillis();
            Map smallIndexGas = viewHourGasMapper.getSmallIndexGas(sql.toString());
            System.err.println("执行耗时 : " + (System.currentTimeMillis() - a) / 1000f + " 秒 ");
            if (smallIndexGas.get("sumNum") != null && smallIndexGas.get("countNum") != null && Integer.parseInt(smallIndexGas.get("countNum").toString()) != 0)
                resultMap.put(String.valueOf(x), Double.valueOf(smallIndexGas.get("sumNum").toString()) / Double.valueOf(smallIndexGas.get("countNum").toString()));
        }
        //求出值别平均值
        Set strings = resultMap.keySet();
        Double sumD = 0.0;
        for (String string : strings) {
            sumD += resultMap.get(string);
        }
        resultMap.put("loopAverage", sumD / loop);
        return resultMap;
    }

}

然后使用并执行

SmallIndicatorThread so2Thread = new SmallIndicatorThread(groupStringMap, loop, "view_hour_gas", "出口SO2浓度", longOrgCode,viewHourGasMapper);
        SmallIndicatorThread smokeThread = new SmallIndicatorThread(groupStringMap, loop, "view_hour_gas", "出口烟尘浓度", longOrgCode,viewHourGasMapper);
        SmallIndicatorThread noxThread = new SmallIndicatorThread(groupStringMap, loop, "view_hour_gas", "出口NOx浓度", longOrgCode,viewHourGasMapper);
        ForkJoinTask> S02t = forkJoinPool.submit(so2Thread);
        ForkJoinTask> smoket = forkJoinPool.submit(smokeThread);
        ForkJoinTask> noxt = forkJoinPool.submit(noxThread);

执行后检查是否都执行完毕

private void validateTheard(ForkJoinTask> ot) {

        while (true) {
            if (ot.isDone() && !ot.isCancelled()) {
                break;
            } else {
                try {
                    Thread.sleep(1);//避免cpu高速运转
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

执行完毕后取值

try {
            So2 = S02t.get();
            smoke = smoket.get();
            nox = noxt.get();
            so2Shft = so2S.get();
            smokeShft = smokeS.get();
            noxShft = noxS.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

你可能感兴趣的:(使用ForkJoinPool完成多线程配置)