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

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
D
Docker
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
美团技术团队
V
V2EX
Last Week in AI
Last Week in AI
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
B
Blog RSS Feed
博客园_首页
B
Blog
博客园 - 叶小钗
I
InfoQ
WordPress大学
WordPress大学
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
The Register - Security
The Register - Security
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Latest news
Latest news
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News and Events Feed by Topic
S
Secure Thoughts
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News

博客园 - 飘渺峰

一致性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的情况。