参数配置,只能配置核心线程池参数大小
@Bean(name = "threadPoolTaskScheduler") public ThreadPoolTaskScheduler taskScheduler() { // Spring 默认配置是核心线程数大小为1,最大线程容量大小不受限制,队列容量也不受限制。 ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); // 核心线程数 scheduler.setPoolSize(config.getCoreSize()); //executor.setCorePoolSize(config.getCoreSize()); // 最大线程数 //executor.setMaxPoolSize(config.getMaxSize()); // 队列大小 //executor.setQueueCapacity(config.getQueueCapacity()); // 当最大池已满时,此策略保证不会丢失任务请求,但是可能会影响应用程序整体性能。 //new ThreadPoolExecutor.AbortPolicy() scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); scheduler.setThreadNamePrefix(config.getThreadNamePrefix()); //表明是否等待所有线程执行完任务 scheduler.setWaitForTasksToCompleteOnShutdown(true); //等待的时间,超过这个时间就强制销毁,因为不能无限的等待下去以确保应用最后能够被关闭 scheduler.setAwaitTerminationSeconds(600); scheduler.setRemoveOnCancelPolicy(Boolean.TRUE); scheduler.setErrorHandler((throwable) ->{ logger.error("====" + throwable.getMessage() + "====", throwable); }); scheduler.initialize(); return scheduler; }
public void initialize() { if (logger.isInfoEnabled()) { logger.info("Initializing ExecutorService" + (this.beanName != null ? " '" + this.beanName + "'" : "")); } if (!this.threadNamePrefixSet && this.beanName != null) { setThreadNamePrefix(this.beanName + "-"); } this.executor = initializeExecutor(this.threadFactory, this.rejectedExecutionHandler); }
@Override protected ExecutorService initializeExecutor( ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler); if (this.removeOnCancelPolicy) { if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(true); } else { logger.debug("Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor"); } } return this.scheduledExecutor; }
ThreadPoolTaskScheduler中创建
@Override protected ExecutorService initializeExecutor( ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { this.scheduledExecutor = createExecutor(this.poolSize, threadFactory, rejectedExecutionHandler); if (this.removeOnCancelPolicy) { if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) { ((ScheduledThreadPoolExecutor) this.scheduledExecutor).setRemoveOnCancelPolicy(true); } else { logger.debug("Could not apply remove-on-cancel policy - not a ScheduledThreadPoolExecutor"); } } return this.scheduledExecutor; }
protected ScheduledExecutorService createExecutor( int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { return new ScheduledThreadPoolExecutor(poolSize, threadFactory, rejectedExecutionHandler); }
ScheduledThreadPoolExecutor中创建
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue(), threadFactory, handler); }