Spring线程池和自定义线程池的使用

1、下面是自定义的线程池的代码

public class SettThreadPoolExecutor {
    private static final Log LOG = LogFactory.getLog(SettThreadPoolExecutor.class);
    /**
     * 针对核心Thread的Max比率 ,以10为基数,8表示0.8
     */
    private int notifyRadio = 4;

    /**
     * 最少线程数.
* 当池子大小小于corePoolSize就新建线程,并处理请求. */
private int corePoolSize; /** * 线程池缓冲队列大小.
* 当池子大小等于corePoolSize,把请求放入workQueue中,池子里的空闲线程就去从workQueue中取任务并处理. */
private int workQueueSize; /** * 最大线程数.
* 当workQueue放不下新入的任务时,新建线程入池,并处理请求,
* 如果池子大小撑到了maximumPoolSize就用RejectedExecutionHandler来做拒绝处理. */
private int maxPoolSize; /** * 允许线程闲置时间,单位:秒.
* 当池子的线程数大于corePoolSize的时候,多余的线程会等待keepAliveTime长的时间,如果无请求可处理就自行销毁. */
private long keepAliveTime; private ThreadPoolExecutor executor = null; public void init() { if (workQueueSize < 1) { workQueueSize = 1000; } if (this.keepAliveTime < 1) { this.keepAliveTime = 1000; } int coreSize = 0; if (this.corePoolSize < 1) { coreSize = Runtime.getRuntime().availableProcessors(); maxPoolSize = Math.round(((float) (coreSize * notifyRadio)) / 10); corePoolSize = coreSize / 4; if (corePoolSize < 1) { corePoolSize = 1; } } // NOTICE: corePoolSize不能大于maxPoolSize,否则会出错 if (maxPoolSize < corePoolSize) { maxPoolSize = corePoolSize; } /** * ThreadPoolExecutor就是依靠BlockingQueue的阻塞机制来维持线程池,当池子里的线程无事可干的时候就通过workQueue.take()阻塞住 */ BlockingQueue notifyWorkQueue = new ArrayBlockingQueue(workQueueSize); executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, notifyWorkQueue, new ThreadPoolExecutor.CallerRunsPolicy()); LOG.info("NotifyExecutor Info : CPU = " + coreSize + " | corePoolSize = " + corePoolSize + " | maxPoolSize = " + maxPoolSize + " | workQueueSize = " + workQueueSize); } public void destroy() { executor.shutdownNow(); } public void execute(Runnable command) { executor.execute(command); } public void showExecutorInfo() { LOG.info("NotifyExecutor Info : corePoolSize = " + corePoolSize + " | maxPoolSize = " + maxPoolSize + " | workQueueSize = " + workQueueSize + " | taskCount = " + executor.getTaskCount() + " | activeCount = " + executor.getActiveCount() + " | completedTaskCount = " + executor.getCompletedTaskCount()); } public void setNotifyRadio(int notifyRadio) { this.notifyRadio = notifyRadio; } public void setWorkQueueSize(int workQueueSize) { this.workQueueSize = workQueueSize; } public void setKeepAliveTime(long keepAliveTime) { this.keepAliveTime = keepAliveTime; } public void setCorePoolSize(int corePoolSize) { this.corePoolSize = corePoolSize; } public void setMaxPoolSize(int maxPoolSize) { this.maxPoolSize = maxPoolSize; } }

2、线程池在Spring-context.xml中调用

        
    <bean id="settThreadPoolExecutor" class="com.roncoo.pay.app.settlement.utils.SettThreadPoolExecutor" init-method="init" destroy-method="destroy">
        
        <property name="corePoolSize" value="5" />
        
        <property name="maxPoolSize" value="10" />
        
        <property name="workQueueSize" value="256" />
        
        <property name="keepAliveTime" value="3" />
    bean>


    
    <bean id="threadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        
        <property name="corePoolSize" value="20" />
        
        <property name="keepAliveSeconds" value="30000" />
        
        <property name="maxPoolSize" value="200" />
        
        <property name="queueCapacity" value="1000" />
    bean>

你可能感兴趣的:(线程池)