欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线程池配置

程序员文章站 2022-05-01 14:19:30
...

ThreadPoolConfig

package com.newland.iot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class ThreadPoolConfig {
    @Value("${spring.executor.core.pool.size:500}")
    private Integer corePoolSize;
    @Value("${spring.executor.max.pool.size:0x7fffffff}")
    private Integer maxPoolSize;
    @Value("${spring.executor.queue.capacity:0x7fffffff}")
    private Integer queueCapacity;

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(corePoolSize);
        taskExecutor.setMaxPoolSize(maxPoolSize);
        taskExecutor.setQueueCapacity(queueCapacity);
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        return taskExecutor;
    }
}

使用

@Component
public class TestTaskExecutor {

    @Autowired
    private TaskExecutor taskExecutor;

    taskExecutor.execute(() -> System.out.println("taskExecutor...");
}

 

相关标签: 线程池