0Pricing
System Design Basics for Backend Developers · درس

التزامن والتنفيذ المتوازي

افهم كيفية الاستفادة من التزامن والتنفيذ المتوازي لتشغيل مهام متعددة في الوقت نفسه وتحسين استخدام الموارد.

التزامن والتنفيذ المتوازي درس مجاني في System Design Basics for Backend Developers على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في System Design Basics for Backend Developers، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة System Design Basics for Backend Developers 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Multi-Tasking Systems

Ever notice how your computer can play music, download files, and browse the web all at the same time? This isn't magic; it's the power of multi-tasking!

In system design, we often need our applications to handle many operations efficiently. This is where the concepts of concurrency and parallelism become crucial.

Understanding Concurrency

Concurrency is about dealing with many things at once. Think of a chef juggling multiple cooking tasks in a single kitchen.

  • The chef might chop vegetables, then stir a pot, then check an oven.
  • They're not doing everything simultaneously, but they're making progress on several tasks by switching between them quickly.
  • This gives the illusion of simultaneous execution.

Concurrency in Software

In software, concurrency often means a single CPU core rapidly switches between different tasks or threads. This is called context switching.

  • One task runs for a short period.
  • The CPU saves its state and switches to another task.
  • This happens so fast that users perceive tasks running "at the same time."

It improves responsiveness and allows a system to make progress on multiple operations.

Concurrent Task Demo

Here's a simple Java example showing two "tasks" running concurrently. The main thread starts two new threads, and the operating system or JVM schedules them to run.

Notice how their output might interleave, showing that they are making progress without necessarily finishing one before starting the other.

public class ConcurrencyDemo {
  public static void main(String[] args) {
    Runnable task1 = () -> {
      for (int i = 0; i < 3; i++) {
        System.out.println("Task A: " + i);
        try { Thread.sleep(50); } catch (InterruptedException e) {}
      }
    };

    Runnable task2 = () -> {
      for (int i = 0; i < 3; i++) {
        System.out.println("Task B: " + i);
        try { Thread.sleep(50); } catch (InterruptedException e) {}
      }
    };

    new Thread(task1).start();
    new Thread(task2).start();
    System.out.println("Main thread done.");
  }
}

True Parallelism

Parallelism is about doing many things at once, literally simultaneously. Imagine having multiple chefs, each with their own kitchen, working on different dishes at the exact same time.

  • Each chef (or CPU core) executes a task independently.
  • This requires multiple processing units (like multiple cores in a CPU).
  • It's about increasing throughput by truly executing multiple instructions at the same instant.

Parallelism in Action

For true parallelism, your system needs multiple processing units. Modern CPUs have multiple cores, allowing multiple threads to run simultaneously.

If you run the previous Java example on a multi-core processor, the operating system might schedule Task A on one core and Task B on another, leading to actual simultaneous execution.

This is different from concurrency on a single core, which simulates simultaneous execution through rapid switching.

Concurrency vs. Parallelism

Let's clarify the key difference:

  • Concurrency: Deals with many tasks at once, often by switching between them. (e.g., one CPU core handling multiple threads).
  • Parallelism: Does many tasks at once, literally simultaneously. (e.g., multiple CPU cores each handling a thread).

A system can be concurrent without being parallel (single-core CPU). A parallel system is always concurrent (it's dealing with multiple tasks).

Boosting System Performance

Both concurrency and parallelism are vital for high-performance systems:

  • Improved Responsiveness: Concurrent systems can keep the user interface active while background tasks run.
  • Higher Throughput: Parallel systems can process more requests or data in a given time, utilizing all available CPU power.
  • Better Resource Utilization: They make efficient use of CPU cores, especially in servers handling many client connections.

Managing the Complexity

While powerful, concurrency and parallelism introduce challenges:

  • Race Conditions: When multiple threads access shared resources, the final outcome depends on their execution order, leading to unpredictable results.
  • Deadlocks: Two or more threads get stuck waiting for each other to release resources, causing the system to halt.
  • Complexity: Designing and debugging concurrent/parallel systems is harder due to non-deterministic behavior.

Careful synchronization and design patterns are needed to mitigate these issues.

Concurrency vs. Parallelism Check

Consider a web server running on a single-core CPU that handles multiple client requests by rapidly switching between them. Which of the following best describes this scenario?

Recap: Concurrency & Parallelism

We've explored concurrency, which is about managing multiple tasks by switching between them, and parallelism, which is about executing multiple tasks truly simultaneously using multiple processing units.

Both are fundamental for designing high-performance, responsive, and scalable backend systems, though they introduce complexities like race conditions and deadlocks that require careful handling.

الأسئلة الشائعة

هل درس «التزامن والتنفيذ المتوازي» مجاني؟

نعم — نص درس «التزامن والتنفيذ المتوازي» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة System Design Basics for Backend Developers، انتقل إلى CoddyKit PRO. تتضمن دورة System Design Basics for Backend Developers 4 دروس في المجموع.

ماذا ستتعلم في «التزامن والتنفيذ المتوازي»؟

افهم كيفية الاستفادة من التزامن والتنفيذ المتوازي لتشغيل مهام متعددة في الوقت نفسه وتحسين استخدام الموارد. تتمرن على System Design Basics for Backend Developers مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ System Design Basics for Backend Developers؟

لا تُشترط خبرة سابقة. System Design Basics for Backend Developers على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «التزامن والتنفيذ المتوازي»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس System Design Basics for Backend Developers هذا؟

نعم. كل درس في System Design Basics for Backend Developers يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تحسين زمن الاستجابة ومعدل النقل
  2. التزامن والتنفيذ المتوازي
  3. اختبار الأداء وتحليله
  4. تجميع اتصالات قاعدة البيانات
← العودة إلى System Design Basics for Backend Developers