Skip to content
The Chat Archive
gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)

package cn.asktrue.framework.config.threadpool; import lombok.extern.slf4j.Slf4j; import…

  1. User

    package cn.asktrue.framework.config.threadpool;

    import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

    import java.util.concurrent.ThreadPoolExecutor;

    /**

    • @class ThreadPoolConfig

    • @author: wangjing

    • @date: 2020/11/5/0005

    • @desc: TODO

    • 当需要实现并发、异步等操作时,通常都会使用到多线程

    • 1、当任务是非必要的时候。比如记录操作日志、通知第三方服务非必要信息等,可以使用线程池处理非阻塞任务

    • 2、当任务非常耗时时候,可以采用线程池技术

    • 3、当请求并发很高时,可以采用线程池技术优化处理 */ @Slf4j @Configuration //@ConfigurationProperties(prefix = "project.threadpool")//前缀 //@PropertySource(value = "classpath:project.properties")//配置文件路径 public class ThreadPoolConfig {

      // 核心线程池大小 @Value("${project.threadpool.corePoolSize}") private int corePoolSize;

      // 最大可创建的线程数 @Value("${project.threadpool.maxPoolSize}") private int maxPoolSize;

      // 队列最大长度 @Value("${project.threadpool.queueCapacity}") private int queueCapacity;

      // 线程池维护线程所允许的空闲时间 @Value("${project.threadpool.keepAliveSeconds}") private int keepAliveSeconds;

      // 设置线程池中任务的等待时间 @Value("${project.threadpool.awaitTerminationSeconds}") private int awaitTerminationSeconds;

      @Bean("threadPoolExecutor") public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setKeepAliveSeconds(keepAliveSeconds); executor.setThreadNamePrefix("threadPoolExecutor>>>"); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(awaitTerminationSeconds); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); log.info("ThreadPool init ! "); return executor; }

    }

  2. Assistant

    The ThreadPoolConfig class is a configuration class for setting up a thread pool in a Spring application. Here's a breakdown of what's going on:

    1. Annotations:

      • @Slf4j: This annotation is used from the Lombok library. It automatically generates a logger field (log) in the class, allowing for easy logging.
      • @Configuration: Indicates that the class contains one or more Spring bean definitions. It is a configuration class used by Spring's IoC container to instantiate, configure, and assemble the beans.
    2. Configuration Properties:

      • The class uses @Value annotations to inject properties from the application's configuration file (e.g., application.properties or application.yaml), specifically under the prefix project.threadpool.
    3. ThreadPoolTaskExecutor Bean:

      • A ThreadPoolTaskExecutor bean named threadPoolExecutor is defined.
      • The executor is configured with properties for core pool size, max pool size, queue capacity, keep-alive time, etc.
      • setThreadNamePrefix sets a prefix for the names of threads created by the pool.
      • setWaitForTasksToCompleteOnShutdown and setAwaitTerminationSeconds are used to handle graceful shutdown of the executor.
      • setRejectedExecutionHandler defines a handler for tasks that cannot be executed by the thread pool immediately. CallerRunsPolicy handles such tasks by running them in the calling thread.
      • executor.initialize() is called to initialize the executor.
    4. Logging:

      • A log statement indicates when the thread pool has been initialized.

    This thread pool can be useful for executing concurrent tasks in a Spring application, especially for non-blocking operations or high-concurrency scenarios. Adjust the pool properties based on the application's needs and available resources for optimal performance.