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

结论

方法有很多,目的都是修改线程工厂类里面的 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 @ 2023-03-07 20:34  aaacarrot  阅读(504)  评论(0编辑  收藏  举报