Parallel.For(每个)和并行化程度 [英] Parallel.For(each) and degrees of parallelisation

查看:115
本文介绍了Parallel.For(每个)和并行化程度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个循环中有4000个要做的事情,我想同时做一些事情。我相信Parallel.For缩放到我们服务器上的内核数量是48.我不希望那些在打开网络连接等的同时运行,我感觉事情无法处理下游所以好吧。



你会怎么做?一次最多说五件事?尽可能简单?

I've got 4000 things to do in a loop, and I'd like to do some of them at the same time. I believe Parallel.For scales to the number of cores which on our server is 48. I don't want that many running at the same time as they open network connections etc. and I have a feeling things couldn't handle it downstream so well.

How would you do this? Say up to five things at once? As simply as possible?

推荐答案

你可以设置 MaxDegreesOfParallelism [ ^ ]选项。



我建议的替代选项是查看Threadpool类,因为你也可以限制它管理的线程数。



ThreadPool.SetMaxThreads [ ^ ]
you could set the MaxDegreesOfParallelism[^] option in your Parrallel.For loop.

Alternative options I could suggest is look at the Threadpool Class as you are also able to limit the number of threads that it manages.

ThreadPool.SetMaxThreads[^]


我将4000个对象分成五个obj列表ects,并将每个列表交给单独的BackgroundWorker进行处理。
I'd divide the 4000 objects into five lists of objects, and hand each list to a separate BackgroundWorker to process.


这篇关于Parallel.For(每个)和并行化程度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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