Parallel

Parallel is a concept in programming that involves executing multiple tasks simultaneously to maximize resource utilization and improve program efficiency. It's a common approach for handling independent tasks and is supported by various programming languages and frameworks.

Key Features of Parallel:

  1. Concurrent Execution: Parallel allows multiple tasks to execute concurrently, reducing wait times and optimizing resource usage.

  2. Parallelism: Tasks are executed in parallel, potentially across multiple threads or CPU cores, depending on the implementation provided by the programming language or framework.

  3. Independence: Tasks in Parallel are typically independent of each other, meaning one task doesn't need to wait for another to complete before starting.

Example Illustrating Parallel Usage in C#

In C#, the System.Threading.Tasks namespace provides methods to support parallel execution using the Parallel class or methods like Parallel.ForEach, Parallel.For, and Parallel.Invoke.

using System;
using System.Threading.Tasks;

public class Program
{
    public static void Main()
    {
        // Example using Parallel.For to iterate over a range of values
        Parallel.For(0, 10, i =>
        {
            Console.WriteLine($"Task {i} is executing on thread {Task.CurrentId}");
            // Simulate some work
            Task.Delay(100).Wait();
        });

        Console.WriteLine("Parallel.For has completed.");

        // Example using Parallel.ForEach to iterate over an array
        string[] names = { "Alice", "Bob", "Charlie", "David" };
        Parallel.ForEach(names, name =>
        {
            Console.WriteLine($"Hello, {name}!");
            // Simulate some work
            Task.Delay(100).Wait();
        });

        Console.WriteLine("Parallel.ForEach has completed.");

        // Example using Parallel.Invoke to execute multiple actions in parallel
        Parallel.Invoke(
            () => DoWork("Task 1"),
            () => DoWork("Task 2"),
            () => DoWork("Task 3")
        );

        Console.WriteLine("Parallel.Invoke has completed.");
    }

    private static void DoWork(string taskName)
    {
        Console.WriteLine($"{taskName} is executing on thread {Task.CurrentId}");
        // Simulate some work
        Task.Delay(100).Wait();
        Console.WriteLine($"{taskName} has completed.");
    }
}

Detailed Explanation

  • Parallel.For: Used to iterate over a range of values and perform tasks concurrently.

  • Parallel.ForEach: Used to iterate over an array (or collection) and perform tasks concurrently for each element in the array.

  • Parallel.Invoke: Used to execute a list of functions or methods concurrently.

In the example:

  • Tasks are executed concurrently on different threads (due to Task.Delay and Wait simulating processing time), and the program doesn't wait for each task to complete.

Summary

Parallel is an approach to executing tasks simultaneously, improving performance and optimizing resource utilization in multithreaded programming. It's beneficial when handling multiple independent tasks concurrently without needing to wait for each task to finish individually.

Last updated