java中thread执行mapper service方法

废话没有直接上demo,就是撸!
thread类如下:

package com.zdmoney.credit.service.suanhua.impl;

import com.zdmoney.credit.common.util.SpringContextUtil;
import com.zdmoney.credit.dao.framework.ShCreaditPlm7020Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: zhangjiachen
 * @Date: 2018/10/18 19:29
 * @Description:
 */
public class SuanHuaPlm7020Thread implements Runnable {

    private static Logger logger = LoggerFactory.getLogger(SuanHuaPlm7020Thread.class);


    private ShCreaditPlm7020Mapper shCreaditPlm7020Mapper = (ShCreaditPlm7020Mapper) SpringContextUtil.getBean
            ("shCreaditPlm7020Mapper");

    private String startNumber;
    private String endNumber;
    private String clienPort;

    public SuanHuaPlm7020Thread(String startNumber, String endNumber, String clienPort) {
        this.startNumber = startNumber;
        this.endNumber = endNumber;
        this.clienPort = clienPort;
    }

    @Override
    public void run() {
        logger.info("线程:" + Thread.currentThread().getName() + "开始执行!" + "clienPort=" + clienPort +
                "startNumber=" + startNumber + "endNumber=" + endNumber);

        int count = shCreaditPlm7020Mapper.insertViewData(clienPort, startNumber, endNumber);
        logger.info("线程:" + Thread.currentThread().getName() + "执行完毕,插入了" + count + "条数据!");
    }
}

service如下:直接启动,配合线程池

public void queryViewToLocal(String clientPort) {
        logger.info("查询:" + clientPort + "号端客户");
        //查询所有数据量
        int allDataNumber = shCreaditPlm7020Mapper.selectAllDataNumber(clientPort);
        logger.info("DBLINK返回一共有:" + allDataNumber + "条数据!");
        //计算每个线程执行的条数
        int erverThreadDoNumber = allDataNumber / 4;
        int startNum = 0;
        int endNum = erverThreadDoNumber;
        int threadNum = 4;

        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
        for (int i = 0; i < threadNum; i++) {
            SuanHuaPlm7020Thread thread = new SuanHuaPlm7020Thread(String.valueOf(startNum), String.valueOf(endNum),
                    clientPort);
            executorService.execute(thread);
            startNum += erverThreadDoNumber;
            endNum += erverThreadDoNumber;
            if (3 == i && (allDataNumber % 4) != 0) {
                //如果没有除尽 多余的交给最后一个线程
                endNum += allDataNumber % 4;
            }
        }

你可能感兴趣的:(java日常)