Nowadays we have parallel processors. In order to can use processing power efficiently we have to do parallel programming so that our code execute faster and efficiently.
Before .NET framework 4 we used to do same using threading. Here is example of same
Code 1 Parallelizing a For Loop using .NET framework 3.5 and less
1. int lowerBound = 0, upperBound = N;
2. int numThreads = Environment.ProcessorCount;
3. int chunkSize = (upperBound – lowerBound) / numThreads;
4.
5. var threads = new Thread[numThreads];
6. for (int t = 0; t < threads.Length; t++) {
7. int start = (chunkSize * t) + lowerBound;
8. int end = t < threads.Length – 1 ? start + chunkSize : upperBound;
9.
10. threads[t] = new Thread(delegate() {
11. for (int i = start; i {
2. … // Process i here
3. });

Many times in real time scenarios during parallel processing we require thread specific data (Data for every respective parallel process)

Code 2 Parallelizing a For Loop using .NET framework 4 with option bag
1. var options = new ParallelOptions { MaxDegreeOfParallelism = 4 };
2. Parallel.For(0, N, options, i=> {
3. … // Process i here
4. });
To make it significantly easier for developers to take advantage of parallelism without complicating the programming, but at the same time give more advanced developers the knobs they need to fine-tune the processing and execution.

Code 3  Parallelizing a For Loop using .NET framework 4 with custom execution

1. Parallel.For(0, N, (i,loop) => {
2. … // Process i here
3. if (SomeCondition()) loop.Break();
4. });

The Parallel class provides support not just for integral ranges, but also for arbitrary IEnumerable sources, the .NET Framework representation of an enumerable sequence: code may continually call MoveNext on an enumerator in order to retrieve the next Current value.

Code 4 Parallelizing a For Loop using .NET framework 4 with arbitrary source

1. IEnumerable lines = File.ReadLines(“data.txt”);
2. Parallel.ForEach(lines, line => {
3. … // Process line here
4. });