java线程池工具类

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.*;


/**
 * <b>DESCRIPTION:</b>线程池工具类,可以根据自己的业务生成自己的线程池
 * 具体配置参考properties/threadpool.properties配置文件<br/>
 */
public class CommonThreadPoolUtil {
    private static final String THREAD_POOL_FILE = "properties/threadpool.properties";
    private static final String DEFAULT_PREFIX = "default";
    private static Map<String, ExecutorService> threadMap = new ConcurrentHashMap<String, ExecutorService>();
    private static final RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

    /**
     * @Date 2019/5/11 13:40
     * @Param  * @param preFix
     * @Return java.util.concurrent.ExecutorService
     * 创建线程池,参数线程配置前缀
     */
    public static  ExecutorService createThreadPool(String preFix) throws Exception {

        preFix = preFix == null ? DEFAULT_PREFIX : preFix;
        Properties config = new Properties();
        config.load(CommonThreadPoolUtil.class.getClassLoader().getResourceAsStream(THREAD_POOL_FILE));
        ThreadModel threadModel = new ThreadModel();
        BeanInfo beanInfo = Introspector.getBeanInfo(threadModel.getClass());
        PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
        for(String key : config.stringPropertyNames()) {
            if (key.startsWith(preFix)){
                String fieldName = key.substring(preFix.length()+1);
                for (PropertyDescriptor property : properties) {
                    if (property.getName().equals(fieldName)) {
                        property.getWriteMethod().invoke(threadModel,Integer.parseInt(config.getProperty(key)));
                    }
                }
            }
        }
        return createExecutorService(threadModel);
    }

    public static ExecutorService getExecuteThreadTask(String preFix) {
        preFix = preFix == null ? DEFAULT_PREFIX : preFix;
        return threadMap.get(preFix);
    }

    public static ExecutorService executeThreadTask(Runnable runnable) throws Exception {
        ExecutorService executorService = threadMap.get(DEFAULT_PREFIX);
        if (executorService == null) {
            executorService = createThreadPool(null);
            threadMap.put(DEFAULT_PREFIX, executorService);
        }
        executorService.execute(runnable);
        return executorService;
    }

    /**
     * @Date 2019/5/11 13:41
     * @Param  * @param runnable 执行线程
     * @param prefix 线程配置前缀
     * @Return java.util.concurrent.ExecutorService
     *
     */
    public static ExecutorService executeThreadTask(Runnable runnable,String prefix) throws Exception {

        ExecutorService executorService = threadMap.get(prefix);
        if (executorService == null) {
            executorService = createThreadPool(prefix);
            threadMap.put(prefix,executorService);
        }
        executorService.execute(runnable);
        return executorService;
    }

    private static ExecutorService createExecutorService(ThreadModel threadModel) {
        ExecutorService executorService = new ThreadPoolExecutor(threadModel.getMinSize(),
                threadModel.getMaxSize(), threadModel.getKeepAliveTime(), TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(threadModel.getWorkQueueSize()),
                HANDLER);
        return executorService;
    }

    /**
     * @Author 张凯
     * @Date 2019/5/11 13:42
     * @Param  * @param callable 执行线程
     * @param prefix 线程配置前缀
     * @Return java.util.concurrent.Future
     *
     */
    public static Future executeThreadTask(Callable callable,String prefix) throws Exception {

        ExecutorService executorService = threadMap.get(prefix);
        if (executorService == null) {
            executorService = createThreadPool(prefix);
            threadMap.put(prefix,executorService);
        }
        Future future = executorService.submit(callable);
        return future;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值