Java并发

来源:互联网 发布:中国教育现状数据分析 编辑:程序博客网 时间:2024/06/11 17:13

package com.lv;

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

public class BaseCallable implements Callable{

private CallableBean callableBean;private Object obj;public BaseCallable(CallableBean callableBean) {    this.callableBean = callableBean;}public BaseCallable(CallableBean callableBean,Object obj) {    this.callableBean = callableBean;    this.obj = obj;}@Overridepublic V call()throws NoSuchMethodException,SecurityException,IllegalAccessException,IllegalArgumentException,InvocationTargetException{    V result = null;    Method method;    try {        switch(callableBean.getCallType()){            case ONE:                method = callableBean.getCallCls().getClass().getMethod(callableBean.getMethodNm(),CallableBean.class);                result = (V)method.invoke(callableBean.getCallCls(),callableBean);                break;            case TWO:                method = callableBean.getCallCls().getClass().getMethod(callableBean.getMethodNm(),CallableBean.class,obj.getClass());                result = (V)method.invoke(callableBean.getCallCls(),callableBean,obj);                break;        }    } catch (NoSuchMethodException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (SecurityException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return result;}

}

package com.lv;

public class CallableBean {

private Object callCls;         //调用类,比如serviceprivate String methodNm;        //调用类的方法private CallType callType;      //调用方法的类型  枚举器 CallType.javaprivate Object 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;}

}

package com.lv;

public enum CallType {

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

}

package com.lv;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ConrruntUtil {

private static ExecutorService executors = Executors.newFixedThreadPool(200);public static <V> V process(CallableBean callableBean){    V result = null;    BaseCallable<V> baseCallable = new BaseCallable<V>(callableBean);    FutureTask<V> futureTask = new FutureTask<V>(baseCallable);    executors.submit(futureTask);    try {        result = futureTask.get();    } catch (InterruptedException | ExecutionException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return result;}public static <T> void processForList(CallableBean callableBean,List<T> list){    List<FutureTask<Object>> futureTaskList = new ArrayList<FutureTask<Object>>();    for(final T t : list){        BaseCallable baseCallable = new BaseCallable(callableBean,t);        FutureTask futureTask = new FutureTask(baseCallable);        executors.submit(futureTask);        futureTaskList.add(futureTask);    }    try {        for(FutureTask futureTask : futureTaskList){            futureTask.get(3000,TimeUnit.MILLISECONDS);        }    } catch (InterruptedException | ExecutionException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }catch (TimeoutException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }}

}

原创粉丝点击