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