如何在Java中命名线程池的线程 [英] How to name the threads of a thread pool in Java

查看:193
本文介绍了如何在Java中命名线程池的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Executor框架的Java应用程序,并且我的代码看起来像这样 protected ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5)

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

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

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我需要一种方法来区分由我创建的和由服务器创建的.

我在想,如果我可以命名线程池,它应该可以解决问题,但是看不到任何允许我执行此操作的API.

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.

谢谢.

推荐答案

您可以传递自己的执行器. 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.

这篇关于如何在Java中命名线程池的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆