简体   繁体   English

线程的命名约定?

[英]Naming conventions for threads?

It's helpful to name threads so one can sort out which threads are doing what for diagnostic and debugging purposes. 命名线程是有帮助的,这样就可以理清哪些线程正在做什么用于诊断和调试目的。

Is there a particular naming convention for threads in a heavily multi-threaded application that works better than another? 在多线程应用程序中,线程是否有一个特定的命名约定,它比另一个更好? Any guidelines? 任何准则? What kind of information should go into the name for a thread? 什么样的信息应该进入线程的名称? What have you learned about naming your threads that could be helpful to others? 你有什么了解如何命名你的线程可能对其他人有帮助?

There's to my knowledge no standard. 据我所知,没有标准。 Over the time I've found these guidelines to be helpful: 随着时间的推移,我发现这些指南很有帮助:

  • Use short names because they don't make the lines in a log file too long. 使用短名称,因为它们不会使日志文件中的行太长。

  • Create names where the important part is at the beginning. 创建重要部分位于开头的名称。 Log viewers in a graphical user interface tend to have tables with columns, and the thread column is usually small or will be made small by you to read everything else. 图形用户界面中的日志查看器往往具有包含列的表,并且线程列通常很小,或者由于您要读取其他内容而变小。

  • Do not use the word "thread" in the thread name because it is obvious. 不要在线程名称中使用“thread”一词,因为它很明显。

  • make the thread names easily grep-able. 使线程名称容易grep-able。 Avoid similar sounding thread names 避免类似的发声线程名称

  • if you have several threads of the same nature, enumerate them with IDs that are unique to one execution of the application or one log file, whichever fits your logging habits. 如果您有多个具有相同性质的线程,请使用对应用程序执行或一个日志文件唯一的ID枚举它们,以适合您的日志记录习惯为准。

  • avoid generalizations like "WorkerThread" (how do you name the next 5 worker threads?), "GUIThread" (which GUI? is it for one window? for everything?) or "Calculation" (what does it calculate?). 避免像“WorkerThread”这样的概括(你如何命名接下来的5个工作线程?),“GUIThread”(哪个GUI?是一个窗口?对于一切?)或“计算”(它计算什么?)。

  • if you have a test group that uses thread names to grep your application's log files, do not rename your threads after some time. 如果您有一个使用线程名称来测试应用程序日志文件的测试组,请不要在一段时间后重命名您的线程。 Your testers will hate you for doing so. 你的测试人员会讨厌你这样做。 Thread names in well-tested applications should be there to stay. 经过充分测试的应用程序中的线程名称应保留。

  • when you have threads that service a network connection, try to include the target network address in the thread name (eg channel_123.212.123.3). 当您拥有为网络连接提供服务的线程时,请尝试在线程名称中包含目标网络地址(例如channel_123.212.123.3)。 Don't forget about enumeration though if there are multiple connections to the same host. 如果有多个连接到同一主机,请不要忘记枚举。

If you have many threads and forgot to name one, your log mechanism should output a unique thread ID instead (API-specific, eg by calling pthread_self() ) 如果您有许多线程而忘记命名一个,那么您的日志机制应该输出一个唯一的线程ID(特定于API,例如通过调用pthread_self())

Naming threads is useful and you should follow a naming convention that would for anything else, be that variables, methods or classes. 命名线程很有用,您应该遵循其他任何命名约定,即变量,方法或类。 Name them according to what they do and be succinct. 根据他们的行为命名并简明扼要。 If you ever run into a problem that requires a thread dump it will be nice to look at the name and know where to look in your code for the problem rather than examining stack traces and guessing. 如果您遇到需要线程转储的问题,那么查看名称并知道在代码中查找问题的位置会更好,而不是检查堆栈跟踪和猜测。

The only different is that if there are multiple threads of the same type you really should add an index of some sort as thread names should be unique to satisfy certain APIs. 唯一不同的是,如果存在多个相同类型的线程,您应该添加某种索引,因为线程名称应该是唯一的,以满足某些API。 It can also help with logging if you show the thread name to know how your application behaves with partial execution happening on different threads. 如果您显示线程名称以了解应用程序在不同线程上发生部分执行的行为,它也可以帮助进行日志记录。

while Thorsten's answer is the most comprehensive, you might want to look at how Tomcat names its Threads. 虽然Thorsten的答案是最全面的,但您可能想看看Tomcat如何命名其Threads。 I've found that useful. 我发现这很有用。 We were running multiple threads with a quartz scheduler and many of the naming rules that Thorsten suggests were useful. 我们使用石英调度程序运行多个线程,并且Thorsten建议的许多命名规则都很有用。

Are you going to be using a Thread pool ? 你打算使用线程池吗? If yes, then that will reduce the chances that you can add more useful meta info. 如果是,那么这将减少您添加更多有用元信息的机会。 If not, the sky is the limit to how much useful info you can have. 如果没有,天空是你可以拥有多少有用信息的限制。

What about: 关于什么:

[namespace].[Class][.Class...].[Method][current thread]? [namespace]。[Class] [.class ...]。[方法] [当前线程]?

So you have the names: 所以你有这些名字:

Biz.Caching.ExpireDeadItems1 Biz.Caching.ExpireDeadItems1
Biz.Caching.ExpireDeadItems2 Biz.Caching.ExpireDeadItems2
Biz.Caching.ExpireDeadItems3 Biz.Caching.ExpireDeadItems3

etc., for each thread. 等,每个线程。

Hmmm... In the heavily multi-threaded applications I've written, I've generally had multiple threads executing the same function, so I'm not sure that naming threads is very helpful in that scenario. 嗯...在我编写的大量多线程应用程序中,我通常有多个线程执行相同的功能,所以我不确定命名线程在这种情况下是非常有用的。 That said, I did assign each thread an integer ID which was printed in log messages generated by the thread to aid in debugging. 也就是说,我确实为每个线程分配了一个整数ID,该ID在线程生成的日志消息中打印,以帮助调试。

For other applications that had threads with dedicated unique duties, yeah I named them descriptively... but I didn't do this because it was a threaded application, I did it because it is a coding best-practice to do so. 对于其他具有专用独特职责的线程的应用程序,是的,我用描述性地命名它们......但我没有这样做,因为它是一个线程应用程序,我这样做是因为它是一个编码最佳实践。

I tend to approach naming threads the same as naming methods or variables. 我倾向于将命名线程与命名方法或变量相同。 Pick something that concisely describes the process that the thread is responsible for. 选择一些简洁描述线程负责的过程的东西。 I don't think there's a lot of extra information that you can or should put into a thread-name. 我不认为你可以或应该在线程名称中添加很多额外信息。 Expressive but terse is the primary goal. 表达但简洁是首要目标。

The only convention might be to add an incremented suffix to threads that are part of a pool. 唯一的约定可能是将增加的后缀添加到属于池的线程中。

I've seen several naming conventions for threads in .NET. 我在.NET中看到了几个线程的命名约定。

Some prefer using 't' in the beginning of the name (ex. tMain Thread ) but I don't think it has any real value. 有些人更喜欢在名字的开头使用't'(例如tMain Thread ),但我认为它没有任何实际价值。 Instead why not just use plain descriptive names (line variables) such as HouseKeeping , Scheduler and so on. 相反,为什么不使用简单的描述性名称(行变量),如HouseKeepingScheduler等。

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

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