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版)
集合
105
线程安全集合
这些可变的集合实例可以由多个线程同时更改。线程安全集合混用了细粒度锁和无锁技
术来确保线程阻塞时间最短(通常根本不会阻塞)。对于许多线程安全集合,集合枚举
创建集合的快照,然后枚举该快照。另外,线程安全集合可以从多个线程安全地访问,
这也是它的主要优点。操作只会在短时间内阻塞代码,前提是存在阻塞。
生产者集合与消费者集合
设计这些可变集合实例旨在让生产者(可能有多个)向集合中推入项,同时让消费者
(也可能有多个)从集合中提取项。因此,它们充当生产者代码和消费者代码之间的桥
梁,并且可以选择限制集合中的项的数量。生产者集合(或消费者集合)可以有一个阻
API
或异步
API
,例如,当集合为空时,受阻塞的生产者集合(或消费者集合)将阻
塞调用它们的消费者线程,直到添加另一项为止。但是,异步生产者集合(或消费者集
合)可以让调用它们的消费者线程异步等待,直至添加另一项。
本章的各节使用了不同的生产者集合(或消费者集合),它们具有不同的优点。表
9-1
整理
了一些集合,我们可以根据具体情况进行选择。
9-1:生产者集合与消费者集合
特性 通道
BlockingCollection<T> BufferBlock<T>
AsyncProducer-
ConsumerQueue<T>
AsyncCollection<T>
队列语义
/
背包
语义
同步
API
异步
API
当满载时,
丢弃项
经过微软
测试
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