【线程池】定义线程池名称

结论

方法有很多,目的都是修改线程工厂类里面的 Name 属性
记录一下可能会到用的方法

1、自定义线程工厂,模仿 NamedThreadFactory 自定义写一个就行
2、Google guava 工具类 提供的 ThreadFactoryBuilder
3、Spring 框架提供的 CustomizableThreadFactory
4、Apache commons-lang3 提供的 BasicThreadFactory

代码

1、自定义线程工厂,模仿 NamedThreadFactory 自定义写一个就行

// 实现 ThreadFactory 接口,实现一个线程工厂类,照抄。不用引入新的包,推荐
public class RenameThreadPool01 {
@Test
public void init(){
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 10, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000),
new NamedThreadFactory("mythread"));
threadPoolExecutor.execute(()->{
System.out.println(33366);
System.out.println(1/0);
});
}
private class NamedThreadFactory implements ThreadFactory {
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
NamedThreadFactory(String name) {
SecurityManager s = System.getSecurityManager();
this.group = s != null ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
this.namePrefix = "哦豁-" + name + "-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(this.group, r, this.namePrefix + this.threadNumber.getAndIncrement(), 0L);
t.setDaemon(true);
if (t.getPriority() != 5) {
t.setPriority(5);
}
return t;
}
}
}

2、Google guava 工具类 提供的 ThreadFactoryBuilder

比较方便,但要引入依赖,看情况使用

import com.google.common.util.concurrent.ThreadFactoryBuilder;
public class RenameThreadPool02 {
@Test
public void init(){
ThreadFactory guavaThreadFactory = new ThreadFactoryBuilder().setNameFormat("xxss-pool-").build();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 10, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000),
guavaThreadFactory);
threadPoolExecutor.execute(()->{
System.out.println(33366);
System.out.println(1/0);
});
}
}

3、Spring 框架提供的 CustomizableThreadFactory

比较方便,但要引入依赖,看情况使用

import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
public class RenameThreadPool03 {
@Test
public void init(){
ThreadFactory springThreadFactory = new CustomizableThreadFactory("aaspringThread-pool-");
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 10, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000),
springThreadFactory);
threadPoolExecutor.execute(()->{
System.out.println(33366);
System.out.println(1/0);
});
}
}

4、Apache commons-lang3 提供的 BasicThreadFactory

比较方便,但要引入依赖,看情况使用

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
public class RenameThreadPool04 {
@Test
public void init(){
ThreadFactory basicThreadFactory = new BasicThreadFactory.Builder()
.namingPattern("basicThreadFactory-").build();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 10, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(1000),
basicThreadFactory);
threadPoolExecutor.execute(()->{
System.out.println(33366);
System.out.println(1/0);
});
}
}

暂时记录了这么几种,大同小异。按需实现吧。

posted @   aaacarrot  阅读(797)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示