C# 101: Concurrency vs Parallelism

Concurrency vs Parallelism

并发 vs 并行

Concurrency and parallelism are two terms often used in the context of multi-threading and multi-tasking in computer science, but they refer to different concepts.

并发和并行是计算机科学中与多线程和多任务相关的两个术语,但它们指的是不同的概念。


1. Concurrency (并发)

Definition: Concurrency is the ability to handle multiple tasks or processes at the same time by switching between them, often quickly. It does not mean that the tasks are running simultaneously but that they can make progress within the same time frame.

定义:并发是指能够同时处理多个任务或进程,通常通过在它们之间快速切换。它并不意味着这些任务是同时运行的,而是它们可以在同一时间段内取得进展。

Key Characteristics:

  • Task Switching: Concurrency involves switching between multiple tasks that are progressing but not necessarily executing at the same exact moment.

    任务切换:并发涉及在多个任务之间切换,这些任务在进展,但不一定在同一时间精确执行。

  • Single or Multi-core Systems: Concurrency can be implemented on a single-core system where tasks are time-sliced, or on multi-core systems.

    单核或多核系统:并发可以在单核系统上实现,任务通过时间片轮转完成,也可以在多核系统上实现。

Example:

Imagine a person switching between reading a book and watching TV. The person can’t do both at the same time, but they can switch back and forth between the two tasks.

设想一个人在阅读书籍和看电视之间来回切换。他们不能同时做这两件事,但可以在两者之间切换。

Code Example (Chinese only):

public class ConcurrencyExample
{
    public static async Task DoTask1()
    {
        await Task.Delay(1000); // 模拟长时间运行的任务
        Console.WriteLine("任务1完成");
    }

    public static async Task DoTask2()
    {
        await Task.Delay(500);  // 模拟另一个长时间运行的任务
        Console.WriteLine("任务2完成");
    }

    public static async Task Main()
    {
        // 并发执行任务
        Task task1 = DoTask1();
        Task task2 = DoTask2();

        await Task.WhenAll(task1, task2);  // 等待所有任务完成
    }
}
  • In this example, the two tasks (DoTask1 and DoTask2) run concurrently. While one task is waiting, the other can make progress.

    在这个例子中,两个任务(DoTask1DoTask2)并发运行。当一个任务在等待时,另一个任务可以继续执行。


2. Parallelism (并行)

Definition: Parallelism is the simultaneous execution of multiple tasks or processes. It requires multiple processors or cores, where each core can handle a separate task at the same time.

定义:并行是指多个任务或进程的同时执行。它需要多个处理器或核心,每个核心可以同时处理不同的任务。

Key Characteristics:

  • Simultaneous Execution: Parallelism means tasks are literally running at the same time on different cores or processors.

    同时执行:并行意味着任务确实在不同的核心或处理器上同时运行。

  • Multi-core Systems: Parallelism requires a multi-core system where multiple cores or processors can execute different tasks simultaneously.

    多核系统:并行需要多核系统,不同的核心或处理器可以同时执行不同的任务。

Example:

Imagine a person reading a book and another person watching TV at the same time. Both tasks are happening simultaneously without any switching.

设想一个人在读书,另一个人在看电视。这两件事同时发生,没有任何切换。

Code Example (Chinese only):

public class ParallelExample
{
    public static void DoTask1()
    {
        Console.WriteLine("任务1正在执行");
        System.Threading.Thread.Sleep(1000);  // 模拟长时间运行的任务
        Console.WriteLine("任务1完成");
    }

    public static void DoTask2()
    {
        Console.WriteLine("任务2正在执行");
        System.Threading.Thread.Sleep(500);  // 模拟另一个长时间运行的任务
        Console.WriteLine("任务2完成");
    }

    public static void Main()
    {
        // 并行执行任务
        Parallel.Invoke(DoTask1, DoTask2);
    }
}
  • In this example, DoTask1 and DoTask2 are executed in parallel, meaning both tasks are running at the same time on different threads.

    在这个例子中,DoTask1DoTask2是并行执行的,意味着两个任务在不同的线程上同时运行。


3. Comparison Between Concurrency and Parallelism

并发与并行的比较

Aspect Concurrency (并发) Parallelism (并行)
Definition Multiple tasks making progress simultaneously, but not necessarily at the exact same moment. Multiple tasks running at the same time on different cores or processors.
Execution Model Tasks are switched between each other. Tasks are executed simultaneously.
Hardware Requirement Can be achieved on a single-core system through task switching. Requires multi-core systems or multiple processors.
Task Scheduling Tasks are scheduled and switched between. Tasks run simultaneously on different cores.
Real-World Analogy A single person switching between multiple tasks. Multiple people doing different tasks at the same time.
Example in Programming Async/await in C#, threads that switch tasks. Parallel tasks using Parallel.Invoke, multi-threading.

4. When to Use Concurrency vs Parallelism

何时使用并发与并行

  • Concurrency: Use when you want to manage multiple tasks that are not CPU-bound, such as waiting for user input, file I/O, or network requests. It is ideal for tasks that require waiting or need to be executed over time without being dependent on available CPU cores.

    并发:当你想管理多个非CPU密集型任务时使用,比如等待用户输入、文件I/O或网络请求。并发非常适合那些需要等待或随着时间执行的任务,而不依赖于可用的CPU核心。

  • Parallelism: Use when you have CPU-bound tasks that can be divided into smaller units and executed simultaneously on different processors or cores. This is ideal for tasks that require significant computation, such as mathematical calculations, data processing, or simulations.

    并行:当你有CPU密集型任务并且可以将其分成较小的单元并在不同的处理器或核心上同时执行时使用。并行非常适合需要大量计算的任务,比如数学运算、数据处理或仿真。


Summary

  • Concurrency is about managing multiple tasks by making progress on them, often by switching between tasks. It doesn’t mean simultaneous execution.

    并发是通过在多个任务之间切换来管理多个任务的进展。这并不意味着同时执行。

  • Parallelism is about executing multiple tasks simultaneously on different processors or cores.

    并行是指在不同的处理器或核心上同时执行多个任务。

By understanding these two concepts, you can choose the right approach based on the nature of your tasks and the available hardware.

通过理解这两个概念,你可以根据任务的性质和可用的硬件选择合适的处理方式。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *