ThreadPoolTaskScheduler spring定时任务框架,线程池配置

参数配置,只能配置核心线程池参数大小

@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);
    }
​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值