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版)
122
9
// 在添加
13
之前,该添加操作等待
7
被移除
queue.Add(13);
queue.CompleteAdding();
讨论
当生产者比消费者运行得快时,节流就很重要了。我们必须考虑这个问题:当应用程序在
其他与本地硬件不同的硬件上运行时,生产者是否会比消费者运行得快?为了确保应用程
序能在未来的硬件和云实例上运行,一定程度的节流通常是很有必要的。相比开发机来
说,这些环境的限制往往更多。
节流会对生产者造成背压,通过减缓它们来确保消费者能处理所有项,而不会导致内存压
力过大。如果无须处理所有项,那么可以选择采样而不是节流。关于采样生产者队列(或
消费者队列),参阅
9.10
节。
可以从
System.Threading.Channels
NuGet
包中获取通道,从
System.Threading.
Tasks.Dataflow
NuGet
包中获取
BufferBlock<T>
类型,从
Nito.AsyncEx
NuGet
包中获取
AsyncProducerConsumerQueue<T>
类型。
参阅
9.6
节探讨同步生产者队列(或消费者队列)的基本用法。
9.8
节探讨异步生产者队列(或消费者队列)的基本用法。
9.10
节探讨节流的一种替代方案,即采样生产者队列(或消费者队列)。
9.10
 采样队列
问题
假设有生产者队列和消费者队列,但是生产者可能比消费者运行得快,于是造成了不必要
的内存使用。你并不需要保留所有的队列项,现在需要设法过滤队列项,让运行较慢的生
产者只需要处理那些重要项。
解决方案 ...
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