> For the complete documentation index, see [llms.txt](https://idt-nida.gitbook.io/developer-guideline/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://idt-nida.gitbook.io/developer-guideline/must-you-know/comparison/concurrency-vs-parallelism.md).

# Concurrency vs Parallelism

## 🆚 Comparison

| Feature              | Concurrency                                                            | Parallelism                                                                    |
| -------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| Definition           | Managing multiple tasks at the same time.                              | Executing multiple tasks at the same time.                                     |
| Focus                | Structure of the program, dealing with multiple tasks.                 | Execution of tasks simultaneously on multiple cores.                           |
| Task Handling        | Tasks start, run, and complete in overlapping time periods.            | Tasks run at exactly the same time.                                            |
| Context Switching    | Frequent context switching between tasks.                              | Little to no context switching needed.                                         |
| Resource Utilization | Better resource utilization via multitasking.                          | High CPU utilization by running tasks on multiple cores.                       |
| Example              | Handling multiple I/O-bound tasks.                                     | Performing multiple CPU-bound tasks.                                           |
| Programming Model    | Often achieved with threads, coroutines, and asynchronous programming. | Often achieved with parallel loops, GPU processing, and multi-core processing. |
| Goal                 | Improve throughput and responsiveness.                                 | Improve computation speed and reduce execution time.                           |
| Typical Use Cases    | Web servers handling multiple requests, GUI applications.              | Scientific computations, video rendering, real-time simulations.               |
| Performance Gain     | Gains depend on effective management of tasks.                         | Gains depend on the number of available processing units.                      |

## 🅰️ Concurrency

{% hint style="info" %}
**Definition** : การจัดการกับหลายงานในเวลาเดียวกัน

**Focus** : โครงสร้างของโปรแกรมและการจัดการหลายงาน

**Task Handling** : งานเริ่มทำและเสร็จในช่วงเวลาเดียวกัน

**Context Switching** : การเปลี่ยนบริบทระหว่างงานบ่อย

**Resource Utilization** : ใช้ทรัพยากรได้ดีผ่านการทำงานหลายอย่าง

**Example** : การจัดการหลายงานที่เกี่ยวข้องกับการ I/O เช่น การรับคำขอจากเซิร์ฟเวอร์เว็บ

**Programming Model** : ใช้เทคนิคต่าง ๆ เช่น Threads, Coroutines, และ Asynchronous Programming

**Goal** : เพิ่มความสามารถในการตอบสนองและการทำงานร่วมกัน

**Typical Use Cases** : เซิร์ฟเวอร์เว็บที่จัดการคำขอหลายคำขอ, แอพพลิเคชัน GUI

**Performance Gain** : ขึ้นอยู่กับการจัดการงานอย่างมีประสิทธิภาพ เช่น การบริหารการเปลี่ยนบริบท (context switching) และการใช้ทรัพยากรอย่างเหมาะสม
{% endhint %}

## 🅱️ Parallelism

{% hint style="info" %}
**Definition** : การประมวลผลหลายงานพร้อมกันในเวลาเดียวกัน

**Focus** : การประมวลผลงานพร้อมกันบนหลายคอร์

**Task Handling** : งานทำงานพร้อมกันในเวลาเดียวกัน

**Context Switching** : การเปลี่ยนบริบทระหว่างงานน้อยมากหรือไม่มี

**Resource Utilization** : ใช้ CPU สูงโดยการทำงานบนหลายคอร์

**Example** : การประมวลผลที่เกี่ยวข้องกับ CPU เช่น การเรนเดอร์วิดีโอ

**Programming Model** : ใช้เทคนิคต่าง ๆ เช่น Parallel Loops, GPU Processing, และ Multi-Core Processing

**Goal** : เพิ่มความเร็วในการคอมพิวเตอร์และลดเวลาการดำเนินการ

**Typical Use Cases** : การคำนวณทางวิทยาศาสตร์, การเรนเดอร์วิดีโอ, การจำลองเวลาจริง

**Performance Gain** : ขึ้นอยู่กับจำนวนหน่วยประมวลผลที่ใช้ได้ เช่น คอร์ของ CPU หรือ GPU
{% endhint %}
