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版)
138
10
{
matrix.Rotate(degrees);
token.ThrowIfCancellationRequested();
});
}
2
种方法需要更多工作量,编写得也没那么好,这是因为并行循环会把
Operation-
CanceledException
包装到
AggregateException
中。同时,如果将
CancellationToken
作为
ParallelOptions
实例的一部分来传递,那么关于检查令牌的频率,
Parallel
类可能会做出
更明智的决策。鉴于这些原因,最好把令牌作为选项传入。如果把令牌作为选项传入了,
那么也可以把令牌传入循环体,但切勿仅把令牌传入循环体。
通过
WithCancellation
运算符,
PLINQ
也内置了取消支持。
IEnumerable<
int
> MultiplyBy2(IEnumerable<
int
> values,
CancellationToken cancellationToken)
{
return
values.AsParallel()
.WithCancellation(cancellationToken)
.Select(item => item * 2);
}
讨论
对并行工作而言,支持取消是提升用户体验的关键。如果应用程序执行并行工作,就会占
用大量的
CPU
资源,至少在短时间内是这样的。
CPU
占用率高,即便不会影响到在同一
台机器上运行的其他应用程序,从用户角度也是能察觉的。因此,这里的建议是,无须考 ...
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