多线程执行单一方法

1.枚举器

public enum CallType {

    ONE,    //使用一个线程
    TWO,    //for循环中每个循环使用一个线程
    THREE,
    FOUR

}

2. 创建多线程调用bean

public class CallableBean {

    private Object callCls;         //调用类,比如service
    private String methodNm;        //调用类的方法
    private CallType callType;      //调用方法的类型  枚举器 CallType.java
    private Object inParams;        //调用方法入参

    public CallableBean(Object callCls, String methodNm, CallType callType, Object inParams) {
        this.callCls = callCls;
        this.methodNm = methodNm;
        this.callType = callType;
        this.inParams = inParams;
    }

    public Object getCallCls() {
        return callCls;
    }

    public void setCallCls(Object callCls) {
        this.callCls = callCls;
    }

    public String getMethodNm() {
        return methodNm;
    }

    public void setMethodNm(String methodNm) {
        this.methodNm = methodNm;
    }

    public CallType getCallType() {
        return callType;
    }

    public void setCallType(CallType callType) {
        this.callType = callType;
    }

    public Object getInParams() {
        return inParams;
    }

    public void setInParams(Object inParams) {
        this.inParams = inParams;
    }
}

3. 新增并发工具框架

import com.cmb.framework.core.container.SpringContainer;
import org.apache.log4j.Logger;
import org.springframework.core.task.TaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**并发框架工具
 * Created by lvyanghui on 2017/7/21.
 */
public class ConcurrentUtil {

    private static final Logger LOGGER = Logger.getLogger(ConcurrentUtil.class);
    private static TaskExecutor taskExecutor = null;

    static {
        getTaskExecutor();
    }

    /**
     * 获取线程池
     *
     * @return
     */
    private static TaskExecutor getTaskExecutor() {
        if (null == taskExecutor) {
            taskExecutor = (TaskExecutor) SpringContainer.getBean("taskExecutor");
        }

        return taskExecutor;
    }

    /**
     * 需要结果
     * @param callList      callable 的list
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static List<Object> processForRst(List<CallableBean> callList) throws ExecutionException,InterruptedException{
        List<FutureTask> futureTaskList = new ArrayList<FutureTask>();
        for(CallableBean callableBean : callList){
            BaseCallable baseCallable = new BaseCallable(callableBean);
            FutureTask futureTask = new FutureTask(baseCallable);
            taskExecutor.execute(futureTask);
            futureTaskList.add(futureTask);
        }
        List<Object> rstList = new ArrayList<Object>();
        for(FutureTask futureTask : futureTaskList){
            Object result = futureTask.get();
            rstList.add(result);
        }
        return rstList;
    }

    /**
     * 直接调用获取结果,可以日志使用
     * @param callableBean
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static Object process(CallableBean callableBean) throws ExecutionException,InterruptedException{
        BaseCallable baseCallable = new BaseCallable(callableBean);
        FutureTask futureTask = new FutureTask(baseCallable);
        taskExecutor.execute(futureTask);
        return futureTask.get();
    }

    /**不需要结果
     * for循环使用
     * @param callableBean          基础类
     * @param list                  for循环的list
     * @param <T>                   list类的类型
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static <T> void processForNoRst(CallableBean callableBean, List<T> list)  throws ExecutionException,InterruptedException{

        List<FutureTask<Object>> futureTaskList = new ArrayList<FutureTask<Object>>();
        for (final T t : list) {

            BaseCallable baseCallable = new BaseCallable(callableBean, t);
            FutureTask futureTask = new FutureTask(baseCallable);
            taskExecutor.execute(futureTask);
            futureTaskList.add(futureTask);
        }

        for (FutureTask futureTask : futureTaskList) {
            //futureTask.get(3000, TimeUnit.MILLISECONDS); TimeoutException
            futureTask.get();
        }

    }
}

5.代码示例Demo

// 使用多线程获取所有客户分配标志信息
// listInParam 方法入参
		CallableBean CallableMarkInfo = new CallableBean(this,"...方法名...", CallType.ONE,listInParam);
		// 使用多线程获取所有主账户客户核心信息
		CallableBean CallableCoreInfo = new CallableBean(this,"getCoreInfoMap", CallType.ONE,listInParam);
		List<CallableBean> callableList = new ArrayList<CallableBean>();
		callableList.add(CallableMarkInfo);
		callableList.add(CallableCoreInfo);

		List<Object> rstCallableList = ConcurrentUtil.processForRst(callableList);
//		Map<String,CusDtbAndRecycleParmas> markInfoMapV1 = (Map<String, CusDtbAndRecycleParmas>) rstCallableList.get(0);
//		Map<String,CusDtbAndRecycleParmas> coreInfoMapV1 = (Map<String, CusDtbAndRecycleParmas>) rstCallableList.get(1);
		markInfoMap = (Map<String, CusDtbAndRecycleParmas>) rstCallableList.get(0);
		coreInfoMap = (Map<String, CusDtbAndRecycleParmas>) rstCallableList.get(1);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值