封装ScheduledThreadPoolExecutor 线程池实现可取消定时任务
线程池配置
EnhanceThreadPoolConfig
import com.ebmsg.module.admin.schedule.EnhanceScheduledThreadPoolExecutor;
import com.ruoyi.common.utils.Threads;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 相关线程池配置
**/
@Configuration
public class EnhanceThreadPoolConfig {
/**
* 核心线程数 = cpu 核心数 + 1
*/
private final int core = Runtime.getRuntime().availableProcessors() + 1;
/**
* 执行周期性或定时任务
*/
@Bean(name = "enhanceScheduledExecutorService")
protected ScheduledExecutorService enhanceScheduledExecutorService() {
return new EnhanceScheduledThreadPoolExecutor(core,
new