ThreadPoolExecutor使用的时候指定线程前缀,便于问题排查 方法一: ThreadFactory threadFactory = new CustomizableThreadFactory("sjzgThread-"); ThreadPoolExecutor threadPoolZg = new ThreadPoolExecutor(6, 8, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(Integer.MAX_VALUE),threadFactory);
方法二:
1、线程配置类 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; /** * 线程配置信息 **/ @Configuration @EnableAsync public class ThreadPoolConfig { @Bean("sjzgThreadPool") public Executor sjzgThreadPool() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setQueueCapacity(Integer.MAX_VALUE); executor.setCorePoolSize(6); executor.setMaxPoolSize(8); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("sjzgThread-"); // 线程池拒绝策略;AbortPolicy:丢弃并抛出异常;DiscardPolicy:丢弃但不抛异常;DiscardOldestPolicy:丢弃最老的,把新的加进去;CallerRunsPolicy:主线程运行 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.initialize(); return executor; } }
2、使用
@Autowired private Executor sjzgThreadPool;