ThreadPool.QueueUserWorkItem VS Task.Factory.StartNew [英] ThreadPool.QueueUserWorkItem vs Task.Factory.StartNew

查看:948
本文介绍了ThreadPool.QueueUserWorkItem VS Task.Factory.StartNew的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别低于

  ThreadPool.QueueUserWorkItem

VS

  Task.Factory.StartNew

如果上述code被称为500次,需要长时间运行的任务,它意味着所有的线程池中的线程将采取行动?

或将TPL(第二个选项)足够聪明,只是占用线程小于或等于处理器的数量?


解决方案

如果你打算开始与TPL长时间运行的任务,应指定<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcreationoptions.aspx\"><$c$c>TaskCreationOptions.LongRunning,这将意味着它的的安排它的线程池。 (编辑:正如在评论中指出,这的的具体调度决策,而不是一个硬性的保证,但我希望,任何明智的生产计划将避免安排长时间运行的任务在一个线程池。)

您绝对不应该安排大量的线程池自己长时间运行的任务。我相信,这些天线程池的默认大小为pretty大(因为它以这种方式经常被滥用),但根本不应该这样使用。

线程池的一点是要避免的的任务,采取从创建一个新的线程,与他们实际运行的时间比较大的打击。如果任务将运行很长一段时间,创建一个新的线程的影响会比较小呢 - 你不想最终可能耗尽线程池中的线程。 (这是不太可能了,但我的没有的体验它在早期版本的.NET。)

个人,如果我有选择,我肯定会使用TPL,理由是在工作 API是pretty不错 - 但的记得告诉TPL您所期望的任务长时间运行。

编辑:正如在评论中指出,也看到了PFX小组对博客文章

:第三方物流和线程池之间进行选择

  

在最后,我要重申一下CLR团队的线程池开发商已经表示:

 任务,现在排队的工作线程池的preferred方式。


编辑:另外从意见,不要忘记,TPL允许您使用定制调度,如果你真的想...

what is difference between the below

ThreadPool.QueueUserWorkItem

vs

Task.Factory.StartNew

If the above code is called 500 times for some long running task does it mean all the thread pool threads will be taken up?

Or will TPL (2nd option) be smart enough to just take up threads less or equal to number of processors?

解决方案

If you're going to start a long-running task with TPL, you should specify TaskCreationOptions.LongRunning, which will mean it doesn't schedule it on the thread-pool. (EDIT: As noted in comments, this is a scheduler-specific decision, and isn't a hard and fast guarantee, but I'd hope that any sensible production scheduler would avoid scheduling long-running tasks on a thread pool.)

You definitely shouldn't schedule a large number of long-running tasks on the thread pool yourself. I believe that these days the default size of the thread pool is pretty large (because it's often abused in this way) but fundamentally it shouldn't be used like this.

The point of the thread pool is to avoid short tasks taking a large hit from creating a new thread, compared with the time they're actually running. If the task will be running for a long time, the impact of creating a new thread will be relatively small anyway - and you don't want to end up potentially running out of thread pool threads. (It's less likely now, but I did experience it on earlier versions of .NET.)

Personally if I had the option, I'd definitely use TPL on the grounds that the Task API is pretty nice - but do remember to tell TPL that you expect the task to run for a long time.

EDIT: As noted in comments, see also the PFX team's blog post on choosing between the TPL and the thread pool:

In conclusion, I’ll reiterate what the CLR team’s ThreadPool developer has already stated:

Task is now the preferred way to queue work to the thread pool.

EDIT: Also from comments, don't forget that TPL allows you to use custom schedulers, if you really want to...

这篇关于ThreadPool.QueueUserWorkItem VS Task.Factory.StartNew的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆