Skip to Content
C#并发编程经典实例(第2版)
book

C#并发编程经典实例(第2版)

by Stephen Cleary
November 2020
Intermediate to advanced
226 pages
5h 34m
Chinese
Posts & Telecom Press
Content preview from C#并发编程经典实例(第2版)
数据流基础
63
如果要处理更高级的场景,比如有一个处理高强度
CPU
计算的特殊块,那么可以通过设
MaxDegreeOfParallelism
选项来命令该块并行操作其输入数据。在默认情况下,该选项
的值为
1
,即每个数据流块一次只能处理一份数据。
可以将
BoundedCapacity
设置为
DataflowBlockOptions.Unbounded
或者任意大于零的值。下
面的示例允许任意数量的任务同时乘以数据。
var
multiplyBlock =
new
TransformBlock<
int
,
int
>(
item => item * 2,
new
ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded
});
var
subtractBlock =
new
TransformBlock<
int
,
int
>(item => item - 2);
multiplyBlock.LinkTo(subtractBlock);
讨论
MaxDegreeOfParallelism
选项让块内部的并行处理简单易行,但确定哪些块需要使用这个选
项并不简单。这里有一个技巧,那就是在调试器中暂停执行数据流。我们可以在调试器中
看到待处理数据项的数量。如果数量惊人,就意味着可以通过重构或者并行化来加以改善。
对于执行异步处理的数据流块,
MaxDegreeOfParallelism
同样有效 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

解密金融数据

解密金融数据

Justin Pauley
PHP编程:第4版

PHP编程:第4版

Kevin Tatroe, Peter MacIntyre

Publisher Resources

ISBN: 9787115550606