简体   繁体   English

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

[英]Should threads in Java be named for easier debugging?

What are the best practices around thread naming in Java? Java中线程命名的最佳实践是什么? Are there any naming conventions to follow? 是否有任何命名约定?

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). 我会说一般的最佳实践是“选择好名字”,这适用于变量,方法,计算机,甚至可能是儿童(只要问我的儿子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 简而言之,让我们避免真正的LongNamesWithManyProbablyIrreleventClauses-Thread-01
  • Meaningful, but accept that you may have a pool of threads job-01, job-02 有意义,但接受你可能有一个线程池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 倾向于严重排序所以更喜欢Listener-http-01

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. class Thread :每个线程都有一个用于标识目的的名称。 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. 赋予多个线程相同的名称可能会使事情变得更糟,并导致混淆而不是帮助调试。

I work on a system that has several Java processes running. 我在一个运行多个Java进程的系统上工作。 Using log4j, I route all output to a single log file. 使用log4j,我将所有输出路由到单个日志文件。

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. 为了大致了解日志记录输出的来源,我使用log4java的源线程输出标志,它在调试消息之前打印源线程名称。

So for me, it's essential to use good thread names. 所以对我来说,使用好的线程名称是必不可少的。

I work mostly on stand-alone java applications, and sometimes the full thread dump is all we have to debug a problem. 我主要在独立的Java应用程序上工作,有时完整的线程转储是我们调试问题的全部内容。 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. 它们是你需要它们之前没有考虑过的事情之一,所有的线程都被命名为“Thread-145”等。

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. 从名称中清楚地知道您期望在系统中有多少线程可能是值得的,因此您可以稍后判断是否存在“线程泄漏”。

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. 如果你已经使用过Eclipse Debug,并且你的java应用程序已经实现了线程,你可以清楚地看到线程与一些随机名称并行运行,“Thread”后跟一些数字序列。 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. 如果你能给出一个有意义的名字(你可能必须使用一个序列,但这完全可以自定义并依赖于你),它在调试时会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM