Introduction
This content serves as a brief summary and transition. After reading this, you'll be ready to dive into the learning points of multithreading in C#~.
Previously, through 16 articles, we've covered knowledge on multithreading, locks, thread pools, tasks, synchronization, and asynchrony, without yet using the keywords async
and await
.
Some students have asked why open-source projects use async
and await
so frequently; the performance impact of using async
and await
is greater than when not using them; and what are the pipelines, asynchrony, and callbacks in ASP.NET Core?
To clarify all this, it's essential to understand Task
(tasks) well. Don't get caught up in the specific keywords, and there's no need to dive into others' projects and lose your hair guessing why they wrote their code that way.
Some classmates mentioned that the examples in my articles do not include output results. This is mainly to save space and avoid redundancy, as readers who can write example code that produces results typically don't require output to be shown in the article. Readers are encouraged to write code and test it themselves.
Author's Suggestion
Um, in this series of articles on multithreading, the two most common phrases I've used are "more on this later" and "not going into too much detail on the principles."
The main reason is that many knowledge points are interwoven. If I presented them all together, it would be very chaotic. It's tedious to separate and organize these points. When we learn, some knowledge should be saved for later; don't let concepts you haven't encountered yet block your learning time.
Additionally, learning should progress from easy to difficult. Start by learning on the surface, practicing coding. With enough practice, you'll become familiar with the concepts, and when necessary, you can delve deeper into the principles. Don't attempt to tackle an entire book all at once.
In writing this series, I referenced "C# 7.0 Core Technologies Guide," "C# 7.0 in a Nutshell," "C# Multithreading in Action (Second Edition)," Microsoft's documentation, and resources from Google.
The two books, "C# 7.0 Core Technologies Guide" and "C# 7.0 in a Nutshell," provide supplemental knowledge on multithreading and asynchrony for developers who already have a grasp, but may not be friendly to beginners due to their complex learning paths, which can hinder newcomers. I think the technology guide is better than the nutshell book.
You may find it unnecessary to read "C# Multithreading in Action (Second Edition)"... because this book is based on .NET Fx 4, and many of its methods are outdated. It also barely touches on principled analysis, focusing primarily on examples. The content is not user-friendly for Chinese readers and often lacks application scenarios.
As for Microsoft's documentation, it mainly serves as a reference for the API and explanations, though the Chinese translations can be discouraging. For instance, in a learning example for a read-write lock, you may encounter a mix of Task
code, which can be confusing. This is documentation, not a tutorial, and it doesn't promote systematic learning.
The quality and suitability of the resources and books mentioned above really depend on individual preference and choices.
Review
Since the upcoming content is closely tied to the foundational concepts of tasks we've previously learned, please browse through the first four tutorials and ensure you've written the examples.
- C# Multithreading (13): Task Basics①
- C# Multithreading (14): Task Basics②
- C# Multithreading (15): Task Basics③
Earlier, we discussed that there are four ways to create a task (Task
), with three of them actually setting up work content, and the last one being a custom task configuration.
The following are the four methods identified by number:
// ①
Task task = new Task(Action action);
// ②
Task task = Task.Factory.StartNew(Action action);
// ③
Task task = Task.Run(Action action);
// ④
TaskCompletionSource<int> task = new TaskCompletionSource<int>();
Task<int> myTask = task.Task; // Task myTask = task.Task;</int></int></int>
For the first method, the created task needs to be started using the .Start()
method.
Task.Run()
is a simplified version of Task.Factory.StartNew()
, and both methods create tasks that begin executing immediately, returning the Task
object. Because the task has already started, you cannot utilize the .Start()
method again.
The fourth method is for task encapsulation and flow control, and it does not perform task work itself. This is used infrequently, so it will be overlooked here.
文章评论