惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - 飘渺峰

一致性Hash算法 析构函数和Dispose方法的区别 查看SQLServer的最大连接数 Hash算法-CityHash算法 Hash算法 Sunday算法--C#版 KMP算法--C#版 BoyerMoore(BM)算法--C# HostFileChangeMonitor [转]软件项目管理总体流程设计 全排列和组合算法 生活 负载均衡算法--C#版 结束进程的方法forceStopPackage 【转】OAUTH协议简介 SQL Server FOR XML PATH 语句的应用 nlog轻量级日志组件 反射加载程序集的几个方法的区别 windows 下TCP最大连接数
.net Parallel并行使用注意事项
飘渺峰 · 2014-03-01 · via 博客园 - 飘渺峰

因项目响应过慢,代码优化空间不大,在暂时无法调整系统架构的情况下,只有使用.NET中的TPL解决一些模块耗时过多的问题。但在使用过程中也碰到了一些问题,现在把它写下来,用于备忘。

1. Parallel.ForEach的使用

 1         static void Main(string[] args)
 2         {
 3             //Test();
 4             TestParllel();
 5             Console.ReadLine();
 6         }
 7 
 8         private static void TestParllel()
 9         {
10             var list = new List<int>(6000);
11 
12             for (int i = 0; i < 6000; i++)
13             {
14                 list.Add(i);
15             }
16             Parallel.ForEach(list, (p, state) => { Invoke(p); });
17         }
18 
19         static void Invoke(int i)
20         {
21             Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
22             Thread.Sleep(30000);
23         }

未设置最大线程数的情况下:

a>为设置最大线程的情况下,TPL默认线程数为任务数(系统允许的情况下,设置ThreadPool.SetMaxThreads没有效果)。

b> TPL默认启动5个线程,任务数小于5的话,启动任务数个线程。

c> 如果任务较多,TPL在初始化5个线程后,每隔100毫秒左右新增线程,直到达到最大线程数。如果新增线程的过程中有任务完成,那么就不会新增线程。

缺点:线程数无法控制,容易造成高CPU,系统失去响应。

设置了最大线程数的情况下:

 1         private static void TestParllel()
 2         {
 3             var list = new List<int>(6000);
 4 
 5             for (int i = 0; i < 6000; i++)
 6             {
 7                 list.Add(i);
 8             }
 9             Parallel.ForEach(list, new ParallelOptions { MaxDegreeOfParallelism = 2}, (p, state) => { Invoke(p); });
10         }

设置了最大线程数为2

a> 系统运行可控,不会造成高CPU的情况。