线程池配置
程序员文章站
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...");
}
上一篇: Python系列之进程池与线程池
下一篇: spring-boot开启异步线程