有没有当它是preferable使用一个普通的旧线程对象,而不是较新的构造之一任何情况下?它是、线程、一个普通、而不是

2023-09-03 08:21:57 作者:綻放的煙花

我看到很多人在博客和这里所以无论是避免或反对类在最近的C#版本的使用建议(我的意思是的当然4.0+,增加任务与朋友们)。即使前,大约有一个事实,即一个普通的老线程的功能,可以通过线程池类来代替在许多情况下辩论。

I see a lot of people in blog posts and here on SO either avoiding or advising against the usage of the Thread class in recent versions of C# (and I mean of course 4.0+, with the addition of Task & friends). Even before, there were debates about the fact that a plain old thread's functionality can be replaced in many cases by the ThreadPool class.

此外,其他专门机构进一步渲染类缺乏吸引力,如定时器取值更换丑陋 + 睡眠组合,而对于图形用户界面,我们有的BackgroundWorker 等等。

Also, other specialized mechanisms are further rendering the Thread class less appealing, such as Timers replacing the ugly Thread + Sleep combo, while for GUIs we have BackgroundWorker, etc.

不过,却是一个非常熟悉的概念,对一些人(包括我自己),人的是,当有涉及某种并行执行的任务,面临,直接跳转到使用好老类。我一直想知道最近如果它的时间来修改我的方式。

Still, the Thread seems to remain a very familiar concept for some people (myself included), people that, when confronted with a task that involves some kind of parallel execution, jump directly to using the good old Thread class. I've been wondering lately if it's time to amend my ways.

所以我的问题是,是否有任何情况下,当有必要或有用使用普通的老式对象,而不是上述的结构之一?

So my question is, are there any cases when it's necessary or useful to use a plain old Thread object instead of one of the above constructs?

推荐答案

Thread类不能过时,因为很明显它是一个的的实施细节的所有,你说那些其它模式。

The Thread class cannot be made obsolete because obviously it is an implementation detail of all those other patterns you mention.

但是,这并不是真正的你的问题;你的问题是

But that's not really your question; your question is

还有什么情况下,当有必要或有用使用普通的老Thread对象,而不是上述的结构之一?

are there any cases when it's necessary or useful to use a plain old Thread object instead of one of the above constructs?

当然。在precisely的情况下,更高级别的结构中有一项不符合您的需求。

Sure. In precisely those cases where one of the higher-level constructs does not meet your needs.

我的建议是,如果你发现自己在一个情况下存在较高的抽象工具不能满足您的需求,并且您希望使用线程实现解决方案,那么你应该的确定缺少的抽象,你真的需要的,然后的使用线程的实施是抽象的,然后的使用抽象的。

My advice is that if you find yourself in a situation where existing higher-abstraction tools do not meet your needs, and you wish to implement a solution using threads, then you should identify the missing abstraction that you really need, and then implement that abstraction using threads, and then use the abstraction.