Spring线程池ThreadPoolTaskExecutor配置及详情

           
    
        
        
        
        
        
        
        
        
        
        
            
        
    

看看源码

package org.springframework.scheduling.concurrent;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.core.task.AsyncListenableTaskExecutor;
import org.springframework.core.task.TaskRejectedException;
import org.springframework.scheduling.SchedulingTaskExecutor;
import org.springframework.util.Assert;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureTask;


@SuppressWarnings("serial")
public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport
		implements AsyncListenableTaskExecutor, SchedulingTaskExecutor {

	private final Object poolSizeMonitor = new Object();

	private int corePoolSize = 1;

	private int maxPoolSize = Integer.MAX_VALUE;

	private int keepAliveSeconds = 60;

	private int queueCapacity = Integer.MAX_VALUE;

	private boolean allowCoreThreadTimeOut = false;

	private ThreadPoolExecutor threadPoolExecutor;


	/**
	 * Set the ThreadPoolExecutor's core pool size.
	 * Default is 1.
	 * 

This setting can be modified at runtime, for example through JMX. */ public void setCorePoolSize(int corePoolSize) { synchronized (this.poolSizeMonitor) { this.corePoolSize = corePoolSize; if (this.threadPoolExecutor != null) { this.threadPoolExecutor.setCorePoolSize(corePoolSize); } } } /** * Return the ThreadPoolExecutor's core pool size. */ public int getCorePoolSize() { synchronized (this.poolSizeMonitor) { return this.corePoolSize; } } /** * Set the ThreadPoolExecutor's maximum pool size. * Default is {@code Integer.MAX_VALUE}. *

This setting can be modified at runtime, for example through JMX. */ public void setMaxPoolSize(int maxPoolSize) { synchronized (this.poolSizeMonitor) { this.maxPoolSize = maxPoolSize; if (this.threadPoolExecutor != null) { this.threadPoolExecutor.setMaximumPoolSize(maxPoolSize); } } } /** * Return the ThreadPoolExecutor's maximum pool size. */ public int getMaxPoolSize() { synchronized (this.poolSizeMonitor) { return this.maxPoolSize; } } /** * Set the ThreadPoolExecutor's keep-alive seconds. * Default is 60. *

This setting can be modified at runtime, for example through JMX. */ public void setKeepAliveSeconds(int keepAliveSeconds) { synchronized (this.poolSizeMonitor) { this.keepAliveSeconds = keepAliveSeconds; if (this.threadPoolExecutor != null) { this.threadPoolExecutor.setKeepAliveTime(keepAliveSeconds, TimeUnit.SECONDS); } } } /** * Return the ThreadPoolExecutor's keep-alive seconds. */ public int getKeepAliveSeconds() { synchronized (this.poolSizeMonitor) { return this.keepAliveSeconds; } } /** * Set the capacity for the ThreadPoolExecutor's BlockingQueue. * Default is {@code Integer.MAX_VALUE}. *

Any positive value will lead to a LinkedBlockingQueue instance; * any other value will lead to a SynchronousQueue instance. * @see java.util.concurrent.LinkedBlockingQueue * @see java.util.concurrent.SynchronousQueue */ public void setQueueCapacity(int queueCapacity) { this.queueCapacity = queueCapacity; } /** * Specify whether to allow core threads to time out. This enables dynamic * growing and shrinking even in combination with a non-zero queue (since * the max pool size will only grow once the queue is full). *

Default is "false". * @see java.util.concurrent.ThreadPoolExecutor#allowCoreThreadTimeOut(boolean) */ public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) { this.allowCoreThreadTimeOut = allowCoreThreadTimeOut; } @Override protected ExecutorService initializeExecutor( ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { BlockingQueue queue = createQueue(this.queueCapacity); ThreadPoolExecutor executor = new ThreadPoolExecutor( this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler); if (this.allowCoreThreadTimeOut) { executor.allowCoreThreadTimeOut(true); } this.threadPoolExecutor = executor; return executor; } /** * Create the BlockingQueue to use for the ThreadPoolExecutor. *

A LinkedBlockingQueue instance will be created for a positive * capacity value; a SynchronousQueue else. * @param queueCapacity the specified queue capacity * @return the BlockingQueue instance * @see java.util.concurrent.LinkedBlockingQueue * @see java.util.concurrent.SynchronousQueue */ protected BlockingQueue createQueue(int queueCapacity) { if (queueCapacity > 0) { return new LinkedBlockingQueue(queueCapacity); } else { return new SynchronousQueue(); } } /** * Return the underlying ThreadPoolExecutor for native access. * @return the underlying ThreadPoolExecutor (never {@code null}) * @throws IllegalStateException if the ThreadPoolTaskExecutor hasn't been initialized yet */ public ThreadPoolExecutor getThreadPoolExecutor() throws IllegalStateException { Assert.state(this.threadPoolExecutor != null, "ThreadPoolTaskExecutor not initialized"); return this.threadPoolExecutor; } /** * Return the current pool size. * @see java.util.concurrent.ThreadPoolExecutor#getPoolSize() */ public int getPoolSize() { if (this.threadPoolExecutor == null) { // Not initialized yet: assume core pool size. return this.corePoolSize; } return this.threadPoolExecutor.getPoolSize(); } /** * Return the number of currently active threads. * @see java.util.concurrent.ThreadPoolExecutor#getActiveCount() */ public int getActiveCount() { if (this.threadPoolExecutor == null) { // Not initialized yet: assume no active threads. return 0; } return this.threadPoolExecutor.getActiveCount(); } @Override public void execute(Runnable task) { Executor executor = getThreadPoolExecutor(); try { executor.execute(task); } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } @Override public void execute(Runnable task, long startTimeout) { execute(task); } @Override public Future submit(Runnable task) { ExecutorService executor = getThreadPoolExecutor(); try { return executor.submit(task); } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } @Override public Future submit(Callable task) { ExecutorService executor = getThreadPoolExecutor(); try { return executor.submit(task); } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } @Override public ListenableFuture submitListenable(Runnable task) { ExecutorService executor = getThreadPoolExecutor(); try { ListenableFutureTask future = new ListenableFutureTask(task, null); executor.execute(future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } @Override public ListenableFuture submitListenable(Callable task) { ExecutorService executor = getThreadPoolExecutor(); try { ListenableFutureTask future = new ListenableFutureTask(task); executor.execute(future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } /** * This task executor prefers short-lived work units. */ @Override public boolean prefersShortLivedTasks() { return true; } }

属性字段说明

corePoolSize:线程池维护线程的最少数量

keepAliveSeconds:允许的空闲时间

maxPoolSize:线程池维护线程的最大数量

queueCapacity:缓存队列,默认值。Integer.MAX_VALUE;

从源码可以看出当值<0的是后就退化为

SynchronousQueue否则为LinkedBlockingQueue,

rejectedExecutionHandler:对拒绝task的处理策略

在实战开发中会用到自定义rejectedExecutionHandler,就是任务太多导致线程池中queue已经塞满的拒绝策略,spring提供了这么几个策略:

  • ThreadPoolExecutor.AbortPolicy策略,是默认的策略,处理程序遭到拒绝将抛出运行时 RejectedExecutionException;
  • ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃;
  • ThreadPoolExecutor.DiscardPolicy策略,不能执行的任务将被丢弃;
  • ThreadPoolExecutor.DiscardOldestPolicy策略,如果执行程序尚未关闭,则位于工作队列头部的任务将被删除,然后重试执行程序(如果再次失败,则重复此过程)。

当然可以自定义拒绝策略和java中自定义的套路一样RejectExecutionHandler

还提供了比较有意思的几种机制:

同步:

execute(Runnable task)
execute(Runnable task, long startTimeout)

异步:

Future submit(Runnable task)
Future submit(Callable task) 

异步回调;

ListenableFuture submitListenable(Runnable task)
ListenableFuture submitListenable(Callable task)

你可能感兴趣的:(Spring框架)