Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 14 章 并发和异步 并发和异步

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

大多数应用程序都需要同时处理多件事情(并发)。在本章中,我们将首先介绍基本的先决条件,即线程和任务的基础知识,然后详细介绍异步的原理和 C# 的异步函数。

第 21 章中,我们将更详细地重温多线程,在第 22 章中,我们将介绍并行编程的相关主题。

导言

以下是最常见的并发情况:

编写响应式用户界面
在 Windows Presentation Foundation (WPF)、移动和 Windows Forms 应用程序中,您必须与运行用户界面的代码同时运行耗时的任务,以保持响应速度。
允许同时处理请求
在服务器上,客户端请求可能同时到达,因此必须并行处理以保持可扩展性。如果您使用 ASP.NET Core 或 Web API,运行时会自动为您完成这项工作。不过,您仍然需要注意共享状态(例如,使用静态变量进行缓存的效果)。
并行编程
在多核/多处理器计算机上执行密集计算的代码,如果在内核之间分配工作负载,执行速度会更快(第 22 章专门讨论了这一点)。
投机执行
在多核计算机上,有时可以通过预测可能需要做的事情并提前做来提高性能。LINQPad 就是利用这种技术来加快创建新查询的速度。另一种方法是并行运行多个不同的算法来解决相同的任务。哪种算法先完成,哪种算法就 "获胜"--当你无法提前知道哪种算法执行得最快时,这种方法就很有效。

程序同时执行代码的一般机制称为多线程。多线程由 CLR 和操作系统支持,是并发的基本概念。因此,了解线程的基本原理,尤其是线程对共享状态的影响,是至关重要的

穿线

线程是一条执行路径,可以独立于其他线程执行。

每个线程都在操作系统进程中运行,该进程为程序的运行提供了一个隔离环境。对于单线程程序,只有一个线程在进程的隔离环境中运行,因此该线程可以独占该环境。多线程程序则是多个线程在一个进程中运行,共享相同的执行环境(尤其是内存)。这就是多线程有用的部分原因:例如,一个线程可以在后台获取数据,而另一个线程可以在数据到达时显示数据。这些数据被称为共享状态

创建主题

客户端程序(控制台、WPF、UWP 或 Windows 窗体)在操作系统自动创建的单线程("主 "线程)中启动。在这里,它作为一个单线程应用程序运行,除非你另辟蹊径,直接或间接创建更多线程。1

你可以通过实例化Thread 对象并调用其Start 方法来创建并启动一个新线程。 Thread 最简单的构造函数需要一个ThreadStart 委托:这是一个无参数方法,指示执行应从何处开始。下面是一个示例:

// NB: All samples in this chapter assume the following namespace imports:
using System;
using System.Threading; Thread t = new Thread (WriteY); // Kick off a new thread t.Start(); // running WriteY() // Simultaneously, do something on the main thread. for (int i = 0; i < 1000; i++) Console.Write ("x"); void WriteY() { for (int i = 0; i < 1000; i++) Console.Write ...
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

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038