ThreadPoolExecutor指定线程名称前缀

发布时间:2023年12月20日
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;
文章来源:https://blog.csdn.net/s07aser123/article/details/135104285
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。