C# 线程的命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/149932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Naming conventions for threads?
提问by core
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?
在多线程应用程序中,是否有特定的线程命名约定,它比其他应用程序运行得更好?有什么指导方针吗?什么样的信息应该进入线程的名称?关于命名可能对其他人有帮助的线程,您学到了什么?
采纳答案by Thorsten79
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.
make the thread names easily 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.
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?).
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 (e.g. channel_123.212.123.3). Don't forget about enumeration though if there are multiple connections to the same host.
使用短名称,因为它们不会使日志文件中的行太长。
在重要部分位于开头的地方创建名称。图形用户界面中的日志查看器往往有带有列的表格,并且线程列通常很小,或者您会变小以阅读其他所有内容。
不要在线程名称中使用“线程”一词,因为它很明显。
使线程名称易于 grep-able。避免听起来相似的线程名称
如果您有多个相同性质的线程,请使用适合您的日志记录习惯的应用程序的一次执行或一个日志文件的唯一 ID 来枚举它们。
避免诸如“WorkerThread”(您如何命名接下来的 5 个工作线程?)、“GUIThread”(哪个 GUI?它用于一个窗口?所有东西?)或“计算”(它计算什么?)之类的概括。
如果您有一个使用线程名称来 grep 应用程序日志文件的测试组,请不要在一段时间后重命名您的线程。你的测试人员会因为你这样做而讨厌你。经过良好测试的应用程序中的线程名称应该留在那里。
当您有为网络连接提供服务的线程时,请尝试在线程名称中包含目标网络地址(例如 channel_123.212.123.3)。如果有多个连接到同一主机,请不要忘记枚举。
If you have many threads and forgot to name one, your log mechanism should output a unique thread ID instead (API-specific, e.g. by calling pthread_self() )
如果您有许多线程而忘记命名一个,您的日志机制应该输出一个唯一的线程 ID(特定于 API,例如通过调用 pthread_self() )
回答by paxos1977
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.
对于其他具有专用独特职责的线程的应用程序,是的,我对它们进行了描述性命名......但我没有这样做是因为它是一个线程应用程序,我这样做是因为它是一种编码最佳实践。
回答by Mike Deck
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.
唯一的约定可能是向作为池一部分的线程添加递增后缀。
回答by William
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. 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.
唯一不同的是,如果有多个相同类型的线程,您确实应该添加某种索引,因为线程名称应该是唯一的以满足某些 API。如果您显示线程名称以了解您的应用程序在不同线程上发生部分执行时的行为方式,它还可以帮助记录日志。
回答by anjanb
while Thorsten's answer is the most comprehensive, you might want to look at how Tomcat names its 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 的回答是最全面的,但您可能想看看 Tomcat 如何命名其线程。我发现这很有用。我们使用石英调度程序运行多个线程,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.
你打算使用线程池吗?如果是,那么这将减少您添加更多有用元信息的机会。如果没有,您可以拥有多少有用的信息就无从谈起。
回答by core
What about:
关于什么:
[namespace].[Class][.Class...].[Method][current thread]?
[命名空间].[类][.类...].[方法][当前线程]?
So you have the names:
所以你有名字:
Biz.Caching.ExpireDeadItems1
Biz.Caching.ExpireDeadItems2
Biz.Caching.ExpireDeadItems3
Biz.Caching.ExpireDeadItems1
Biz.Caching.ExpireDeadItems2
Biz.Caching.ExpireDeadItems3
etc., for each thread.
等等,对于每个线程。
回答by Dror Helper
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. Instead why not just use plain descriptive names (line variables) such as HouseKeeping, Schedulerand so on.
有些人更喜欢在名称的开头使用“t”(例如tMain Thread),但我认为它没有任何实际价值。相反,为什么不使用简单的描述性名称(行变量),例如HouseKeeping、Scheduler等。