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版)
集合
125
相同的生产者代码会按需异步等待:
// 该添加操作立即完成
await
_asyncStack.AddAsync(7);
// 在将
13
加入队列之前,该添加操作异步等待
7
被移除
await
_asyncStack.AddAsync(13);
_asyncStack.CompleteAdding();
消费者示例代码使用了
OutputAvailableAsync
,它也有
9.8
节提到的局限性。如果有多个
消费者,那么消费者代码看起来通常如下。
while
(
true
)
{
int
item;
try
{
item =
await
_asyncStack.TakeAsync();
}
catch
(InvalidOperationException)
{
break
;
}
Trace.WriteLine(item);
}
讨论
AsyncCollection<T>
BlockingCollection<T>
的异步版,两者的
API
略有不同。
可以从
Nito.AsyncEx
NuGet
包中获取
AsyncCollection<T>
参阅
9.7
节探讨阻塞栈和阻塞背包。
9.8
节探讨异步队列,它相比异步栈或异步背包更为常用。
9.12
 阻塞异步队列
问题
假设需要一个回路,可以在不同的代码片段间传递信息或数据,具有先进先出的行为,同时
126
9
具有一定的灵活性,既能将生产者端或消费者端当成是同步的,也能将它们当成是异步的。
比如,背景线程可能正在加载数据并将其推入回路 ...
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