The MaxDegreeOfParallelism sets the maximum number of simultaneous threads that will be used for the Parallel.For(). It does not mean that only two threads will ever be used.

Different threads can be allocated from the threadpool during execution of the Parallel.For(), since threadpool threads are specifically designed to be reused.

The following program demonstrates. If you run it, you'll see that the total number of different threads being used can exceed 2, but the total number of threads being used simultaneously never exceeds 2.

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks; namespace ConsoleApp1
{
class Program
{
static void Main()
{
ParallelOptions po = new ParallelOptions
{
MaxDegreeOfParallelism = 2
}; var activeThreads = new ConcurrentDictionary<int, bool>(); Parallel.For(0, 100, po, x =>
{
activeThreads[Thread.CurrentThread.ManagedThreadId] = true;
Console.WriteLine("Active threads: " + string.Join(", ", activeThreads.Keys));
Thread.Sleep(200);
activeThreads.TryRemove(Thread.CurrentThread.ManagedThreadId, out bool unused);
}); Console.ReadLine();
}
}
}

TPL中限制进程数量的更多相关文章

  1. Python 中的进程与 锁

    理论知识 操作系统背景知识 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操 ...

  2. Python程序中的进程操作-进程池(multiprocess.Pool)

    目录 一.进程池 二.概念介绍--multiprocess.Pool 三.参数用法 四.主要方法 五.其他方法(了解) 六.代码实例--multiprocess.Pool 6.1 同步 6.2 异步 ...

  3. 系统中sshd进程的查看,数量统计,进程号输出,进程清理命令

    1. 查看sshd进程 ps -ef|grep sshd ps -ef:打开所有的进程 grep sshd:过滤出含有“sshd”字符的进程. 2. 查看sshd进程中的sftp进程,不含查询的进程 ...

  4. #调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著,但并不是越多越好),加上verbose=True,显示进程使用信息

    #调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著) from sklearn import datasets X, y = datasets.make_c ...

  5. python中的进程、线程(threading、multiprocessing、Queue、subprocess)

    Python中的进程与线程 学习知识,我们不但要知其然,还是知其所以然.你做到了你就比别人NB. 我们先了解一下什么是进程和线程. 进程与线程的历史 我们都知道计算机是由硬件和软件组成的.硬件中的CP ...

  6. 【转】关于loadrunner中设置进程和线程的区别

    loadrunner中,在进行运行设置中有一项选择,是按进程运行Vuser或按线程运行Vuser?下面进行分别来讲: 1.按进程运行Vuser:Controller将使用驱动程序mdrv运行Vuser ...

  7. 【Linux】 linux中的进程信息相关的一些内容

    _ linux进程信息 ■ top top命令用于动态地查看系统的进程和其他一些资源的信息.开启top的时候可以加上-t <sec>来设置top更新的频率高低.进入top界面之后,可以输入 ...

  8. C#:多进程开发,控制进程数量

    正在c#程序优化时,如果多线程效果不佳的情况下,也会使用多进程的方案,如下: System.Threading.Tasks.Task task=System.Threading.Tasks.Task. ...

  9. 解决CentOS 7中php-fpm进程数过多导致服务器内存资源消耗较大的问题

    本文由荒原之梦原创,原文链接:http://zhaokaifeng.com/?p=653 什么是php-fpm: php-fpm即FastCGI进程管理器,用于控制php的内存和进程等. 操作环境: ...

随机推荐

  1. 毕设demo写好

    2015年1月20日 14:41:47 阶段性暂停!! 把运行结果截图给了老师,老师说先整理下文档,然后下学期来了再部署到服务器上. 那么,下学期来了,估计也要把Epm和CR1000什么的搞好了. 先 ...

  2. head命令 tail命令

    head命令 head命令用于显示文件的开头的内容.在默认情况下,head命令显示文件的头10行内容. -n<数字>:指定显示头部内容的行数: -c<字符数>:指定显示头部内容 ...

  3. CCF CSP 201412-2 Z字形扫描

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201412-2 Z字形扫描 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫 ...

  4. pyspider 在ubuntu上安装失败怎么搞?

    pyspider 是一个非常不错的爬虫框架,在ubuntu上安装时报错误: 很明显pycurl的问题,pycurl没安装成功 pycurl 安装错误解决办法: sudo apt-get install ...

  5. linux ncat命令

    netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...

  6. 面试题49:把字符串转换成整数(atoi)

    需要考虑的问题都已在程序中注释 bool isValid; int StrToInt(const char* str) { isValid = false; //不合法情形1:空指针 if (str ...

  7. 【AtCoder】AGC011 E - Increasing Numbers

    题解 题是真的好,我是真的不会做 智商本还是要多开啊QwQ 我们发现一个非下降的数字一定可以用不超过九个1111111111...1111表示 那么我们可以得到这样的一个式子,假如我们用了k个数,那么 ...

  8. php输出多余的空格或者空行

    1,文件是否有bom.可以通过脚步检测,或者利用notepa++打开,查看编码格式. 2.  <?php echo 'something'; ?>  或许是你的php标签外,有空格或者空行 ...

  9. linux kernel.shmall shemax shemin解释

        Linux X86-64操作系统,Oracle 10g数据库,由8G加到16G,把kernel.shmmax参数改到17179869184(16G)后,发现只要修改sga_max_size和s ...

  10. 在Notepad++上搭建Windows下Shell运行环境

    1.安装NotePad++ 第一步是安装Notepad++. 2.安装 Cygwin64 Terminal Cygwin64 Terminal是在Windows环境下,模拟运行Linux命令的软件,很 ...