线程池VS专用线程 - 当以preFER这线程、当以、VS、preFER

2023-09-03 05:30:44 作者:瞪谁谁怀孕

有什么办法(除了形成实际的性能比较测量结果可以是pretty的努力,使他们现实的)或经验法则的时候,我应该停止使用线程池并使用专用的呢?我想,对于长时间运行的工作,最好是使用专用的,因为它不peramently偷一个来自线程池。对于较短的工作,最好是使用了线程池因为创建线程和线程本身占用了大量的资源。

Is there any way (apart form actual perfomance measurements which can be pretty hard to make them realistic) or rule of thumb when I should stop using the ThreadPool and use a dedicated Thread instead? I suppose for long running work it is better to use a dedicated Thread because it doesn't peramently steal one from the ThreadPool. For shorter work it is better use the the ThreadPool because creating threads and the thread itself consumes a lot of resources.

但如果是魔法屏障?我该如何决定使用哪种方法?

But where is the magic barrier? How do I decide which approach to use?

在简单的应用中,可能没有关系了。但我有一个自定义的基于.NET的应用程序框架,处理在一个单一的应用程序可以有一个的很多的插件,在大多数情况下需要做一些工作asynchonously的。我正在考虑引入策略插件的作者时使用的。

In simple applications it might not matter that much. But I am dealing with a custom .NET-based application framework where a single application can have a lot of plugins which in most cases need to do some work asynchonously. And I am thinking about introducing a policy for the plugin authors when to use which.

推荐答案

对于短期运行的任务使用线程池中的线程。

Use thread pool threads for short running tasks.

使用线程,如果你需要长时间运行的任务或需要设置线程的一些特性,例如优先级,前景,文化等。

Use threads if you need a long running task or need to set some properties of the thread such as priority, foreground, culture etc.

如果有可能在.NET 4的默认调度器使用线程池使用任务类,但你可以提供你自己的调度,从而精确控制任务是如何映射到线程。

If possible use the Task class in .NET 4. The default scheduler uses the thread pool, but you may provide your own scheduler and thus control exactly how tasks are mapped to threads.