线程池

package com.fintech.third.utils;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ThreadPoolExecutorUtils {
    
    public final static String THREAD_POOL_CORE_POOL_SIZE = "thread.pool.core.pool.size";
    
    public final static String THREAD_POOL_MAX_IMUM_POOL_SIZE = "thread.pool.max.imum.pool.size";
    
    public final static String THREAD_POOL_KEEP_ALIVE_TIME = "thread.pool.keep.alive.time";
    
    public final static String THREAD_POOL_BLOCKING_QUEUE_NUM = "thread.pool.blocking.queue.num";
    
    private final static Logger logger = LoggerFactory.getLogger(ThreadPoolExecutorUtils.class);
    
    //核心线程数
    private static int CORE_POOL_SIZE = 20;//Integer.parseInt(PropertiesLoadUtil.getPropertiesValue(THREAD_POOL_CORE_POOL_SIZE, SERVER_RESOURCE_NAME));
    
    //最大线程数
    private static int MAX_IMUM_POOL_SIZE = 50;//Integer.parseInt(PropertiesLoadUtil.getPropertiesValue(THREAD_POOL_MAX_IMUM_POOL_SIZE, SERVER_RESOURCE_NAME));
    
    //线程释放空闲时间--当线程池中线程 空闲时间超过此时间且线程池中数量大于核心线程数时会被释放
    private static int KEEP_ALIVE_TIME =120; //Integer.parseInt(PropertiesLoadUtil.getPropertiesValue(THREAD_POOL_KEEP_ALIVE_TIME, SERVER_RESOURCE_NAME));
    
    //线程任务队列数
    private static int BLOCKING_QUEUE_NUM =100; //.parseInt(PropertiesLoadUtil.getPropertiesValue(THREAD_POOL_BLOCKING_QUEUE_NUM, SERVER_RESOURCE_NAME));
    
    //创建线程池
    private static final ThreadPoolExecutor tpe = new ThreadPoolExecutor(CORE_POOL_SIZE,MAX_IMUM_POOL_SIZE,KEEP_ALIVE_TIME,TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>(BLOCKING_QUEUE_NUM),
            new ThreadPoolExecutor.DiscardOldestPolicy());

    private ThreadPoolExecutorUtils(){
        
    }
    
    public final static void execute(Runnable runnable){
        tpe.execute(runnable);
        logger.info("/*******************************************************/");
        logger.info("/******************* current thread pool task size is "+tpe.getTaskCount());
        logger.info("/******************* current thread pool tasking size is "+tpe.getActiveCount());
        logger.info("/******************* current thread pool tasked size is "+tpe.getCompletedTaskCount());
        logger.info("/*******************************************************/");
    }
}

调用示例

 

ThreadPoolExecutorUtils.execute(new Runnable() {
                        @Override
                        public void run() {
                            try {
                               方法;
                            }catch(Exception e) {
                                logger.error("发生异常", e);
                            }
                        }
                    });

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值