在 Java 中重命名线程

发布于 2024-07-12 10:08:12 字数 232 浏览 8 评论 0原文

我正在开发一个项目,该项目正在慢慢变得越来越大,并且许多不同进程使用的活动线程数量正在增加。 最近,我仔细观察了调试器中正在运行的线程,我注意到我的很多第三方库都给它们的线程起了非常糟糕的名称 - Timer-0、qtp0 等。我希望其他开发人员不熟悉使用命名不当的线程可以立即知道正在运行的内容。

有谁知道如何重命名正在运行的线程,而不是为我们正在使用的库编写补丁? 或者这是否是一个好主意? 任何建议,将不胜感激。

I am working on a project that is slowly getting larger and larger and the number of active threads used by many different processes is increasing. Lately, I have been taking a closer look at the running threads in the debugger and I have noticed that a lot of my third party libraries have given very poor names to their threads - Timer-0, qtp0, etc. I want other devs unfamiliar with the poorly named threads to know instantly what is running.

Rather than write patches for the libs we are using, does anyone know how to rename the running threads? Or if this is even a good idea? Any suggestions would be appreciated.

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

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

发布评论

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

评论(7

乖乖 2024-07-19 10:08:12

是的,您可以使用 setName() 方法。

至于这是否是一个好主意,目前尚无定论,但依赖线程名称来实现人类可读的描述之外的任何内容将是一个非常糟糕的决定,并且大多数库都不会这样做。 因此,如果您重命名它们,不太可能破坏任何东西。

Yes, you can change the name of a thread with the setName() method.

As for whether it's a good idea, there's no telling for sure, but relying on the thread name for anything other than a human-readable description would be a very poor decision, and most libraries will not do this. Thus, it's unlikely to break anything if you rename them.

背叛残局 2024-07-19 10:08:12

更改 Java 线程名称

Thread.currentThread().setName("MyThread");

Change Java Thread Name

Thread.currentThread().setName("MyThread");
鯉魚旗 2024-07-19 10:08:12

如果问题出在开源库中,那么最好的解决方案是为这些产品提供补丁以实现更好的线程命名。 除非安全管理器运行,否则您可以通过枚举线程(正如其他人提到的)并对每个线程应用任意名称来重命名任何线程,但这是一种脆弱的命名不属于您自己的线程的方式。 如果第 3 方库更改了其命名,您将必须更改您的代码。

我同意,具有命名不当的线程的第三方库会使理解应用程序中发生的情况变得更加困难。 但是从第三方库重命名线程是有风险的。 如果他们的下一个版本改变了他们命名线程的方式,会发生什么? 如何处理根本不尝试命名线程的第 3 方库?

编辑:添加回以某种方式从末尾消失的文本

If the problem is in open source libraries, then the best solution is to supply patches to those products for better thread naming. Barring a security manager running, you can rename any thread by enumerating threads (as others have mentioned) and applying an arbitrary name to each, but this is a fragile way of naming threads that are not your own. If the 3rd party library changes their naming, you'll have to change your code.

I agree that 3rd party libraries with poorly-named threads make more difficult understanding what is going on in your application. But renaming threads from a 3rd party library is risky. What happens if their next release changes the way they name threads? And how do you handle a 3rd party library that makes no attempt at all to name threads?

EDIT: Add back text that somehow disappeared from the end

暮年 2024-07-19 10:08:12

我命名了我创建的所有线程。 我有太多的应用程序泄漏线程,或者线程失去控制。

你越把java当作一个操作系统,它就越容易管理。 :)

I name all threads I create. I've had too many apps leaking threads, or having threads otherwise spin out of control.

The more you treat java as an operating system, the easier it gets to manage. :)

染火枫林 2024-07-19 10:08:12

您可以使用 Thread.enumerate() 或 Thread.getAllStackTraces() 来获取正在运行的线程的列表。 然后,您可以对每个调用Thread.setName()

我无法告诉你这是否是一个好主意,但我可能会倾向于“不”。 您编写的代码越多,您需要维护的代码就越多。

You can use Thread.enumerate() or Thread.getAllStackTraces() to get a list of the running Threads. You can then call Thread.setName() on each.

I can't tell you whether its a good idea or not but I'd probably lean towards "no". The more code you write, the more code you have to maintain.

风吹短裙飘 2024-07-19 10:08:12

这就引出了围绕线程命名约定的最佳实践问题。 我没有看到任何这方面的社区指导。

This begs the question of best practices around naming conventions for threads. I haven't seen any community guidance in this regard.

抚笙 2024-07-19 10:08:12

setName() 可用于更改线程的名称,但您必须有某种方法来确定应该调用该线程的名称。
getAllStackTraces() 将允许程序举例说明堆栈跟踪,并使用类/方法/文件名作为调用线程的提示。

setName() can be used to change the name of a thread but you have to have some way of determining what the thread should be called.
getAllStackTraces() would allow the program to example the stack trace and use the class/methods/file name names as a hint as to what to call the thread.

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