c# - Parallel.for 循环执行
问题描述
设想
我需要创建并行执行多个函数的n
线程(不等于要执行的函数数)。所以我的代码是
static void Main()
{
Parallel.For(0, 2, i => // it creates 2 threads as number of iterations.why?
{
method1();
method2();
method3();
method4();
method5();
method6();
method7();
method8();
method9();
method10();
});
}
如何以MaxDegreeOfParallelism
最佳方式使用这里的财产?有人可以帮忙吗?
解决方案
看来您正在寻找Parallel.Invoke而不是(我在您的代码Parallel.For
中看不到任何循环):
ParallelOptions options = new ParallelOptions() {
//TODO: carry out experiment on your workstation to find out the right number
MaxDegreeOfParallelism = 4,
};
// Run method1..method10 in parallel while using options
Parallel.Invoke(options,
method1,
method2,
method3,
method4,
method5,
method6,
method7,
method8,
method9,
method10
);
推荐阅读
- python - 使用aiohttp获取多个网站的状态
- html - 带有框阴影css的边框半径
- python - 在 python 函数中使用字符串时接受“接近匹配”?
- solr - 索引 Solr 时如何指定文件类型
- node.js - MongoDb + 猫鼬 | 如何散列密码数组而不是单个密码
- node.js - 为什么 vscode 控制台显示调试失败,即使它没有?
- css - 在测试中验证文本的 CSS 文本转换
- python - 如何从我的字典值中删除 '\n'
- python - 加载数据集时“没有足够的价值来解压” - Allennlp _read
- r - 将奇怪的格式字符日期转换为 R 中的日期