In this lesson, you will learn how to choose between parallelizing operations across a collection versus parallelizing independent logic paths.
When we talk about concurrency, we often want to use all our CPU cores to speed up our programs. We can achieve this in two distinct ways: Data Parallelism and Task Parallelism.
Data Parallelism
Data Parallelism is all about "doing the same thing to many items." Imagine you have a list of 1,000 images that all need to be resized. Since the process for resizing one image is independent of the others, you can split the work so that multiple CPU cores process different chunks of the list simultaneously.
You typically use Parallel.ForEach for this:
List<string> images = GetImages();
Parallel.ForEach(images, image => {
ResizeImage(image);
});
Here, the logic is identical for every piece of data. The system automatically balances the workload across your available cores.
Task Parallelism
Task Parallelism is about "doing different things at the same time." Sometimes, your program has several distinct, unrelated operations that need to run. For example, while your application is fetching data from a database, it might also be calculating a complex mathematical model and logging diagnostics to a file. These aren't the same operation, and they aren't working on a single collection; they are independent "tasks."
You use Task.Run to fork these different logic paths:
var task1 = Task.Run(() => FetchDatabaseData());
var task2 = Task.Run(() => CalculateModel());
var task3 = Task.Run(() => LogDiagnostics());
await Task.WhenAll(task1, task2, task3);
Which should you choose?
- Use Data Parallelism when you have a massive collection of items and the same operation needs to be performed on each one. It is focused on high-throughput processing.
- Use Task Parallelism when you have separate, distinct blocks of logic that can execute independently to improve responsiveness or reduce overall wait time.
Key Takeaway: Data Parallelism scales an identical operation across a collection, while Task Parallelism runs separate, independent logic paths concurrently.