Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
# C并发编程经典实例 (图灵程序设计丛书)
## 【One-Line Pitch】
A practical recipe book for C# developers who want to master modern concurrency — from async/await basics to parallel processing, reactive programming, and dataflow grids — without drowning in low-level thread details. If you write C# applications that need to stay responsive, scale under load, or handle event streams, this book gives you battle-tested patterns for every scenario.
## 【Book Arc】
- **Opening (~0%–12%)**: Debunks common concurrency myths (concurrency ≠ multithreading), introduces the core vocabulary (concurrent, parallel, async, multithreaded), and sets the tone: modern C#/.NET provides high-level abstractions that make concurrency accessible to every developer. The author explicitly avoids legacy `Thread` and `BackgroundWorker` types.
- **Early (~12%–29%)**: Covers async/await fundamentals — creating tasks, returning completed tasks with `Task.FromResult`, waiting for groups with `Task.WhenAll`, avoiding context continuations with `ConfigureAwait(false)`, and handling `async void` exceptions. Then shifts to parallel processing: `Parallel.ForEach`, `Parallel.Invoke`, and PLINQ for data-parallel workloads.
- **Middle (~29%–47%)**: Introduces TPL Dataflow for building dataflow grids and pipelines, then moves into Reactive Extensions (Rx) for event-stream programming — creating observable sequences from events, throttling/sampling fast event streams, and using schedulers for UI-thread marshaling. Includes unit testing strategies for async methods and dataflow grids.
- **Middle (~47%–59%)**: Bridges old and new async patterns by wrapping legacy APM/EAP/callback-based code with `TaskCompletionSource<T>`, and shows how to integrate Rx observables with async/await and dataflow blocks. Covers concurrent collections (blocking queues, async queues) and immutable collections for thread-safe data sharing.
- **Late (~59%–end)**: Covers cancellation (polling, timeouts, injecting cancellation tokens), functional OOP patterns for async interfaces/constructors/properties/events, synchronization primitives (blocking and async locks, signals, throttling), and scheduling to thread pools with custom task schedulers.
## 【Key Takeaways】
- **Concurrency is broader than multithreading** (Early): Modern C# offers async programming, parallel processing, TPL Dataflow, and reactive programming — each suited to different problems. The book's guiding principle: if you find yourself typing `new Thread()`, your code is outdated.
- **`async`/`await` is the foundation for I/O-bound operations** (Early): Use `Task.Run` only for CPU-bound work; for HTTP requests, database calls, and web services, async/await keeps your UI responsive and your server scalable. `ConfigureAwait(false)` in library code avoids UI-context performance degradation.
- **Parallel processing requires independent tasks** (Early): `Parallel.ForEach`, `Parallel.Invoke`, and PLINQ all work best when task blocks are independent. Shared state forces synchronization, which destroys parallelism — design for independence first.
- **Dataflow grids enable complex pipelines** (Middle): TPL Dataflow blocks (BufferBlock, TransformBlock, ActionBlock) can be linked into pipelines with completion propagation and error handling. Use `Encapsulate` to package reusable grid logic into custom blocks.
- **Rx turns events into queryable streams** (Middle): `Observable.FromEventPattern` wraps .NET events into `IObservable<T>` sequences, and operators like `Throttle`, `Sample`, and `Timeout` tame high-frequency event streams. `ObserveOn` marshals notifications to UI threads.
- **`TaskCompletionSource<T>` bridges legacy async patterns** (Middle): APM, EAP, and callback-based code can all be wrapped into awaitable `Task<T>` objects, making old code compatible with modern async/await.
- **Choose collections by access pattern** (Middle): `ConcurrentDictionary<TKey,TValue>` excels for shared mutable state; immutable collections (`ImmutableList<T>`, `ImmutableDictionary<TKey,TValue>`) are better for rarely-modified, multi-threaded reads — but watch the O(log N) indexing cost of `ImmutableList<T>`.
- **Cancellation should be cooperative and pervasive** (Late): Use `CancellationTokenSource` with polling, timeouts, and injection patterns. Async, parallel, and reactive code all support cancellation — but you must design for it from the start.
## 【Reading Tips】
- **Skim Chapter 1** if you're already familiar with async/await basics — the myth-busting is motivational, but the real value starts with Chapter 2's async recipes.
- **Deep-read Chapters 2–3** for the core async/parallel patterns; these are the foundation for everything else. Pay special attention to `ConfigureAwait(false)` and the difference between stopping and cancelling parallel loops.
- **Chapters 4–5 (Dataflow and Rx) are the most conceptually dense** — read them together, as the book frequently cross-references them. The Rx event-stream examples (throttling mouse moves, wrapping timers) are worth working through slowly.
- **Chapter 7 is a practical goldmine** for anyone maintaining legacy code — the `TaskCompletionSource<T>` wrapping patterns are directly applicable to real-world migration scenarios.
- **Use the "参阅" (see also) cross-references** as a navigation tool; the book is structured as independent recipes, so you can jump directly to the pattern you need.
## 【Coverage Limits】
This guide covers the book's progression through async/await, parallel processing, dataflow, reactive programming, collections, and cancellation. The excerpts do not cover the final chapters on synchronization primitives (Chapter 11) and scheduling (Chapter 12) in detail, nor the functional OOP patterns (Chapter 10) beyond their table-of-contents listing.
##
类提供了TryDequeueAsync 成员,可以⽤来避免冗⻓的消费者代码。如果有多个消费者,消费者 代码通常像这样: while (true) { var dequeueResult = await _asyncQueue.TryDequeueAsync(); if (!dequeueResult.Succes...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Loading comments...
Reply to Comment
Edit Comment