Work as iteration.
Simple loop is run from a single thread, where as Parallel class uses multiple threads.
References : https://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.for.aspx
using System; using System.Threading; using System.Threading.Tasks; using static System.Console; namespace Parallel_For_in_CSharp { class Program { void LoopCall() { int count = 10; for (int i = 0; i < count; i++) { WriteLine($"Index:{i}, Thread:{Thread.CurrentThread.ManagedThreadId}"); } } void ParallelCall() { Parallel.For(0, 10, i => { Console.WriteLine($"Index{i}, Thread:{Thread.CurrentThread.ManagedThreadId}"); Thread.Sleep(100); }); } static void Main(string[] args) { Program obj = new Program(); WriteLine("---------Simple Loop Calling---------"); obj.LoopCall(); WriteLine("---------Parallel.For Calling--------"); obj.ParallelCall(); ReadLine(); } } }
//OutPut
---------Simple Loop Calling---------
Index:0, Thread:1
Index:1, Thread:1
Index:2, Thread:1
Index:3, Thread:1
Index:4, Thread:1
Index:5, Thread:1
Index:6, Thread:1
Index:7, Thread:1
Index:8, Thread:1
Index:9, Thread:1
---------Parallel.For Calling--------
Index0, Thread:1
Index2, Thread:3
Index4, Thread:4
Index6, Thread:5
Index8, Thread:6
Index1, Thread:1
Index3, Thread:3
Index5, Thread:4
Index7, Thread:5
Index9, Thread:6
For Parallel Execution in Parallel.For
ConcurrentBag thread safe list in C#
class Program { static void Main(string[] args) { ConcurrentBagbag = new ConcurrentBag ();//Thread Safe list ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = 5; Parallel.For(0, 50, options, o => { bag.Add(o); Console.Write(Thread.CurrentThread.ManagedThreadId + ","); Thread.Sleep(200); }); Console.Read(); } }
Output
1,3,4,5,6,1,3,4,5,6,1,3,4,5,6,1,3,4,5,6,1,3,4,6,5,1,3,5,6,4,1,3,4,6,5,1,3,5,6,4,1,3,5,4,6,1,3,5,6,4
ConcurrentDictionary thread safe dictionary in C#
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConcurrentDictionary_in_Csharp { //https://docs.microsoft.com/en-us/dotnet/standard/collections/thread-safe/how-to-add-and-remove-items class Program { static ConcurrentDictionary<int, Data> dirBag = new ConcurrentDictionary<int, Data>(); static void Main(string[] args) { ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = 5; Parallel.For(0, 20, options, i => { Console.WriteLine($"Index{i}, Thread:{Thread.CurrentThread.ManagedThreadId}"); var data = new Data { ID = i, Information = "information" + i, LastModifiedDate = DateTime.UtcNow }; dirBag.AddOrUpdate( 1, // i data, (k, v) => { //Delegate invoke when value exists with corresponding key. v.LastModifiedDate= DateTime.UtcNow; // update last modified date Console.WriteLine("Collected data in to bag. for ID: " + v.ID + ", LastMofifiedDate: " + v.LastModifiedDate); Console.Write($" Bag count existing: " + dirBag.Count); return v; }); Thread.Sleep(1000); Console.Write($" Bag count : " + dirBag.Count); }); Console.ReadKey(); } } class Data { public int ID { get; set; } public string Information { get; set; } public DateTime LastModifiedDate { get; set; } } }