Java中如何给线程池中的线程命名

发布于 2024-11-02 15:20:25 字数 417 浏览 4 评论 0原文

我有一个使用 Executor 框架的 Java 应用程序,并且我的代码如下所示 protected ScheduledExecutorService ScheduledExecutorService = new ScheduledThreadPoolExecutor(5)

我的理解是,JVM 在内部会创建一个包含 5 个线程的池。现在,当我在探查器中检查执行情况时,我会得到类似 thread-pool2,thread-pool3 等信息。

这些线程池有些是服务器创建的,有些是我创建的我需要一种方法来区分哪些是我创建的,哪些是服务器创建的

我想,如果我可以命名线程池,它应该可以解决问题,但是没有看到任何 API 可以让我做同样的事情。

提前致谢。

I have a Java application that uses the Executor framework and I have code that looks like this
protected ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5)

My understanding is that internally the JVM would create a pool of 5 threads. Now when I check the execution in a profiler, I get something like thread-pool2,thread-pool3 and so on.

Some of these thread pools are created by the server and some are created by me, I need a way to differentiate which were created by me and which were created by the server.

I am thinking that if I can name the thread pools it should do the trick, however do not see any API which would allow me to do the same.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就像说晚安 2024-11-09 15:20:25

您可以将自己的 ThreadFactory 传递给ScheduledThreadPoolExecutor。您的 ThreadFactory 将创建线程并可以为其指定任何您想要的名称。您的 ThreadFactory 还可以重用 执行器。 defaultThreadFactory(),并且仅在返回线程之前更改名称。

You can pass your own ThreadFactory to ScheduledThreadPoolExecutor. Your ThreadFactory will create thread and can give it any name you want. Your ThreadFactory can also reuse Executors.defaultThreadFactory(), and only change the name before returning the thread.

万水千山粽是情ミ 2024-11-09 15:20:25
public class NamedThreadPoolExecutor extends ThreadPoolExecutor {

private static final String THREAD_NAME_PATTERN = "%s-%d";

    public NamedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, final TimeUnit unit,
                               final String namePrefix) {
       super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<>(),
            new ThreadFactory() {

                private final AtomicInteger counter = new AtomicInteger();

                @Override
                public Thread newThread(Runnable r) {
                    final String threadName = String.format(THREAD_NAME_PATTERN, namePrefix, counter.incrementAndGet());
                    return new Thread(r, threadName);
                }
            });
    }

}
public class NamedThreadPoolExecutor extends ThreadPoolExecutor {

private static final String THREAD_NAME_PATTERN = "%s-%d";

    public NamedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, final TimeUnit unit,
                               final String namePrefix) {
       super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<>(),
            new ThreadFactory() {

                private final AtomicInteger counter = new AtomicInteger();

                @Override
                public Thread newThread(Runnable r) {
                    final String threadName = String.format(THREAD_NAME_PATTERN, namePrefix, counter.incrementAndGet());
                    return new Thread(r, threadName);
                }
            });
    }

}
空‖城人不在 2024-11-09 15:20:25

来自 ThreadPoolExecutor 文档

创建新线程
新线程是使用 ThreadFactory 创建的。如果没有另外指定,则使用 E​​xecutors.defaultThreadFactory(),它创建的线程都位于同一 ThreadGroup 中,并具有相同的 NORM_PRIORITY 优先级和非守护进程状态。通过提供不同的 ThreadFactory,您可以更改线程的名称、线程组、优先级、守护进程状态等。如果 ThreadFactory 在通过从 newThread 返回 null 进行询问时未能创建线程,则执行程序将继续,但可能无法执行任何任务。

From the ThreadPoolExecutor documentation:

Creating new threads
New threads are created using a ThreadFactory. If not otherwise specified, a Executors.defaultThreadFactory() is used, that creates threads to all be in the same ThreadGroup and with the same NORM_PRIORITY priority and non-daemon status. By supplying a different ThreadFactory, you can alter the thread's name, thread group, priority, daemon status, etc. If a ThreadFactory fails to create a thread when asked by returning null from newThread, the executor will continue, but might not be able to execute any tasks.

梦情居士 2024-11-09 15:20:25

使用您自己的自定义线程工厂。实现 ThreadFactoryBuilder 来创建自定义线程工厂,它允许您执行以下操作:

  1. 拥有自定义线程名称
  2. 可以选择线程 - 用户或守护线程
  3. 可以选择线程优先级
  4. 可以灵活地设置未捕获的异常处理程序

您可以在您可以使用以下帖子。

http://wilddiary.com/understanding-java-threadfactory-creating-自定义线程工厂/

Use your own custom thread factory. Implement a ThreadFactoryBuilder to create you custom thread factories that allows you to do the following:

  1. Have custom thread names
  2. Have choice of threads - User or Daemon threads
  3. Have choice of Thread Priority
  4. Have flexibility to set uncaught exception handlers

You have a sample ThreadFactoryBuilder implementation in the following post which you can use.

http://wilddiary.com/understanding-java-threadfactory-creating-custom-thread-factories/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文