Java 中的线程是否应该命名以便于调试?

发布于 2024-09-08 04:32:47 字数 39 浏览 4 评论 0原文

Java 中线程命名的最佳实践是什么?是否有任何命名约定可遵循?

What are the best practices around thread naming in Java? Are there any naming conventions to follow?

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

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

发布评论

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

评论(5

你的笑 2024-09-15 04:32:47

我想说,一般的最佳实践是“选择好名字”,这适用于变量、方法、计算机,甚至可能是儿童(只需问我的儿子 Gerund Extravagaza Smith)。

所以,如果可能的话,请为您的线程选择有意义的名称。

假设我们要求文件或计算机的命名约定。您希望有一个普遍适用的答案吗?我认为您需要生成一个您自己的约定,该约定对于您创建的线程类型有意义。

我会遵循以下一般原则:

  • 简而言之,让我们避免 trueLongNamesWithManyProbicallyIrreleventClauses-Thread-01
  • 有意义,但接受您可能有一个线程池 job-01、job-02
  • 首先是最重要的部分

    01-http-监听器 
    01-rmi-监听器
    02-http-监听器
    

    往往排序不好,所以更喜欢 Listener-http-01

I'd say that a general Best Practice is "choose good names", this applies to variables, methods, computers, possibly even children (just ask my son Gerund Extravagaza Smith).

So yes, choose meaningful names for your threads if possible.

Suppose we asked for a naming convention for files or computers. Would you expect there to be a generally applicable answer? I think that you need to generate a convention of your own that makes sense with the kind of threads your create.

I would follow these general principles:

  • Short, lets avoid reallyLongNamesWithManyProbablyIrreleventClauses-Thread-01
  • Meaningful, but accept that you may have a pool of threads job-01, job-02
  • Most significant part first

    01-http-Listener 
    01-rmi-Listener
    02-http-Listener
    

    tends to sort badly so prefer Listener-http-01

嗫嚅 2024-09-15 04:32:47

是的,命名线程当然有助于识别它们,但是有一个警告:确保为它们提供唯一的名称,因为如果您选择(或者可能是偶然),您可以使用相同的名称多线程。

类线程 :每个线程都有一个用于识别目的的名称。多个线程可能具有相同的名称。

给多个线程赋予相同的名称可能会让事情变得更糟并导致混乱,而不是帮助调试。

Yes, naming threads certainly helps in identifying them, but with one caveat: make sure to give them unique names, because you can, if you choose to (or perhaps by accident), use the same name for multiple threads.

class Thread: Every thread has a name for identification purposes. More than one thread may have the same name.

Giving multiple threads the same name may make things worse and cause confusion instead of helping debugging.

何时共饮酒 2024-09-15 04:32:47

我在一个运行多个 Java 进程的系统上工作。使用 log4j,我将所有输出路由到单个日志文件。

为了大致了解日志输出的来源,我使用 log4java 的源线程输出标志,该标志在调试消息之前打印源线程名称。

所以对我来说,使用好的线程名称至关重要。

I work on a system that has several Java processes running. Using log4j, I route all output to a single log file.

To get a general idea of where logging output is coming from, I use log4java's source thread output flag which prints the source thread name before the debug message.

So for me, it's essential to use good thread names.

猫七 2024-09-15 04:32:47

我主要在独立的 java 应用程序上工作,有时完整的线程转储就是我们调试问题所需的全部内容。线程名称在那里也很重要。它们是您在需要它们之前不会真正考虑的事情之一,并且所有线程都被命名为“Thread-145”等。

也就是说,我没有看到任何特定的命名约定。使用您稍后会认识的名称,并且您不太可能重复使用名称。从名称中明确您希望系统中有多少个线程可能是值得的,这样您就可以知道以后是否有“线程泄漏”。

I work mostly on stand-alone java applications, and sometimes the full thread dump is all we have to debug a problem. Thread names are vital there, too. They're one of those things that you don't really think about until you need them, and all your threads are named "Thread-145", etc.

That said, I haven't seen any particular naming conventions. Use something that you'll recognize later, where you aren't likely to re-use names. It might be worthwhile to make it clear from the name how many of that thread you expect to have active in the system, so you can tell if you have "thread leaks" later.

葵雨 2024-09-15 04:32:47

是的,命名线程将是一个很好的做法。如果您使用过 Eclipse Debug 并且您的 java 应用程序实现了线程,您可以清楚地看到并行运行的线程,并带有一些随机名称,“Thread”后跟一些数字序列。如果您可以给出一个有意义的名称(您可能必须再次使用序列,但这完全是可定制的并且取决于您),那么它在调试期间将会很有用。

Yes, naming threads would be a good practice. If you have used Eclipse Debug and if your java application has threads implemented, you can clearly see the threads running in parallel with some random names, "Thread" followed by some number sequence. If you can give a meaningful name (again you may have to use a sequence, but thats entirely customizable and depends on you), it would be useful during Debug time.

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