ThreadPool.QueueUserWorkItem 中的最大排队元素数
我将最大线程设置为 10。然后我使用 ThreadPool.QueueUserWorkItem 添加了 22000 个任务。 运行程序后很可能没有完成所有22000个任务。可用线程排队的任务数量是否有限制?
I set the max thread to 10. Then I added 22000 task using ThreadPool.QueueUserWorkItem.
It is very likely that not all the 22000 task was completed after running the program. Is there a limitation how many task can be queued for avaiable threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要等待所有任务处理完毕,则需要自己处理。 ThreadPool 线程都是后台线程,不会使应用程序保持活动状态。
这是处理此类情况的相对干净的方法:
话虽这么说,如果您有数千个工作项,通常最好将它们“分组”在一起,而不是将它们视为线程池的单独工作项。这是管理项目列表所涉及的一些开销,并且由于您无法一次处理 22000 个项目,因此最好将它们分组为块。每个进程有 50 个左右的单个工作项目可能会对您的整体吞吐量有很大帮助......
If you need to wait for all of the tasks to process, you need to handle that yourself. The ThreadPool threads are all background threads, and will not keep the application alive.
This is a relatively clean way to handle this type of situation:
That being said, it's usually better to "group" together your work items if you have thousands of them, and not treat them as separate Work Items for the threadpool. This is some overhead involved in managing the list of items, and since you won't be able to process 22000 at a time, you're better off grouping these into blocks. Having single work items each process 50 or so will probably help your overall throughput quite a bit...
队列没有实际限制,但池本身不会超过 64 个等待句柄,即活动线程总数。
The queue has no practical limit however the pool itself will not exceed 64 wait handles, ie total threads active.
这是一个依赖于实现的问题,并且该函数的实现随着时间的推移发生了一些变化。但在 .Net 4.0 中,您基本上受到系统内存量的限制,因为任务存储在内存队列中。您可以通过深入研究 Reflector 中的实现来看到这一点。
This is an implementation dependent question and the implementation of this function has changed a bit over time. But in .Net 4.0, you're essentially limited by the amount of memory in the system as the tasks are stored in an in memory queue. You can see this by digging through the implementation in reflector.
来自 ThreadPool 文档:
您是否有可能在处理完所有任务之前退出?
From the documentation of ThreadPool:
Is it possible that you're exiting before all tasks have been processed?