Java并发

BaseCallable类

package com.cmb.dw.rtl.pbcuscomm.util.concurrent;

import org.apache.log4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;

/**线程基础类
 * Created by lvyanghui on 2017/7/21.
 */
public class BaseCallable  implements Callable {

    private static final Logger LOGGER = Logger.getLogger(BaseCallable.class);

    private CallableBean callableBean;              //callable 调用bean
    private Object paramsObj;                             //for循环list中单个对象

    public BaseCallable(CallableBean callableBean) {
        this.callableBean = callableBean;
    }

    public BaseCallable(CallableBean callableBean, Object paramsObj) {
        this.callableBean = callableBean;
        this.paramsObj = paramsObj;
    }

    @Override
    public Object call()throws NoSuchMethodException,IllegalAccessException,InvocationTargetException{
        Object callObj = callableBean.getCallCls();
        String methodNm = callableBean.getMethodNm();
        CallType callType = callableBean.getCallType();

        LOGGER.info("调用callable开始,调用类:" + callObj + "调用方法" + methodNm + "调用类型" + callType);

        long begin = System.currentTimeMillis();

        Object result = null;
        Method method;

        switch(callType){

            case ONE:
                method = callObj.getClass().getMethod(methodNm,CallableBean.class);
                result = method.invoke(callObj,callableBean);
                break;

            case TWO:
                method = callObj.getClass().getMethod(methodNm,CallableBean.class,Object.class);
                result = method.invoke(callObj,callableBean,paramsObj);
                break;

        }

        long end = System.currentTimeMillis();
        LOGGER.info("调用callable结束,用时" + (end - begin) + "ms");

        return result;
    }

}

CallableBean类

package com.cmb.dw.rtl.pbcuscomm.util.concurrent;

/**callable 调用bean
 * Created by lvyanghui on 2017/7/21.
 */
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;
    }
}

CallType类

package com.cmb.dw.rtl.pbcuscomm.util.concurrent;

/**callable 调用类型 枚举器
 * Created by lvyanghui on 2017/7/21.
 */
public enum CallType {

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

}

ConcurrentUtil类

package com.cmb.dw.rtl.pbcuscomm.util.concurrent;

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();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值