ExecutorService线程池执行业务

1、定时器定时执行代码

package com.eam.task;

import com.eam.iot.ThreadPoolServerImpl;
import org.jxstar.task.SystemTask;
import org.jxstar.task.TaskException;

public class IotServerTaskBO extends SystemTask {

    @Override
    public void execute() throws TaskException {

        final ThreadPoolServerImpl impl = new ThreadPoolServerImpl();
        Runnable runnable = new Runnable() {
            public void run() {
                impl.performLockProcess();
            }
        };
        new Thread(runnable).start();

    }
}

2、线程池分配任务

package com.eam.iot;

import org.jxstar.service.BusinessObject;
import org.jxstar.util.config.SystemVar;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadPoolServerImpl extends BusinessObject {

    private Lock lock = new ReentrantLock();    //锁

    /**
     * 执行锁处理
     * @CSDN作者 Nuuuuud
     */
    public boolean performLockProcess() {

        boolean returnBool = true;
        try {
            lock.lock();
            returnBool = new ThreadPoolServerImpl().allocatingThreadPool();
        } catch (Exception e1) {
            returnBool = false;
            e1.printStackTrace();
        } finally {
            lock.unlock();
        }

        _log.showInfo("执行:" + (returnBool ? "成功" : "失败"));

        return returnBool;
    }

    /**
     * 线程任务分配
     * @CSDN作者 Nuuuuud
     */
    public boolean allocatingThreadPool() throws Exception {

        //获取参数,SystemVar是jxstar框架专用包类,根据各自框架Dao层使用获取数据
        String iotTime = SystemVar.getValue("iot.total.time", "30");//距离当前多久前的数据局
        String iotUnit = SystemVar.getValue("iot.total.unit", "MINUTE");//单位 分钟,小时,天
        String mainSQL = "SELECT data_addr FROM v_iot_show_his WHERE addtime > DATE_SUB(NOW(),INTERVAL " + iotTime + " " + iotUnit + " ) GROUP BY data_addr ";
        //执行查询操作,根据Dao层调用类方法
        List<Map<String, String>> mainListMap = _dao.query(_dao.createParam(mainSQL));
        _log.showDebug("size:" + mainListMap.size() + ";" + mainSQL);

        if (mainListMap.isEmpty() || mainListMap.size() == 0) {
            _log.showInfo("=====未查询到需要执行的数据=====");
            return true;
        }
        // 需要的线程池数量
        int threadNum = 17;
        //CountDownLatch latch = new CountDownLatch(threadNum);//线程计数器

        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
        //Future存储执行的线程,便于控制线程状态获取
        List<FutureTask<String>> futures = new ArrayList<FutureTask<String>>(threadNum);

        int excCount = 0;
        for (Map<String, String> mainMap : mainListMap) {
            //获取查询到的数据库值
            String data_addr = mainMap.get("data_addr");

            //线程需要执行的任务
            IotServerImpl iotServerRunnable = new IotServerImpl(data_addr);
            //添加到future控制
            FutureTask<String> future = new FutureTask<String>(iotServerRunnable);
            //线程池执行
            try {
                executorService.submit(future);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            //添加到List中,后续进行线程状态判断。
            futures.add(future);

            excCount++;
        }

        for (Future<String> future : futures) {
            future.get();
        }

        _log.showDebug("运行次数:" + excCount);
        //关闭线程池
        executorService.shutdown();

        return true;
    }


}

3、具体执行任务,数据可通过构造函数传递

package com.eam.iot;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

public class IotServerImpl extends BusinessObject implements Callable<String> {

    public String data_addr;
    //无参构造
    public IotServerImpl() {

    }
    //有参构造
    public IotServerImpl(String data_addr) {
        this.data_addr = data_addr;
    }

    /**
     * 此方法执行实际业务
     * @CSDN作者 Nuuuuud
     */
    public String call() throws Exception {
        long startTime = new Date().getTime();
        DaoParam dp = _dao.createParam("select * from v_iot_show_his where data_addr=?");
        dp.addStringValue(data_addr);
        List<Map<String, String>> listMap = _dao.query(dp);
        long endTime = new Date().getTime();
        _log.showDebug("线程【" + Thread.currentThread().getName() + "】执行时间:" + (endTime - startTime) + ";data_addr:" + data_addr);

        return null;
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值