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.