using System.Collections.Concurrent; using System.Text; Console.WriteLine("----- Program started -----"); Task[] tasks = new Task[1000000]; Random random = new Random(); // int[] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // for (int i = 0; i < tasks.Length; i++) // { // tasks[i] = Task.Run(() => // { // for (int j = 0; j < numbers.Length; j++) // { // Console.WriteLine($"Before ({Thread.CurrentThread.ManagedThreadId}): number[{j}]: {numbers[j]}"); // // Thread.Sleep(random.Next(0, 1000)); // // // not thread-safe // // int oldValue = numbers[j]; // // int newValue = oldValue + random.Next(1, 10); // // numbers[j] = newValue; // // // // Console.WriteLine($"Between ({Thread.CurrentThread.ManagedThreadId}): number[{j}]: {numbers[j]}"); // // // // Thread.Sleep(random.Next(0, 1000)); // // // // Console.WriteLine($"After ({Thread.CurrentThread.ManagedThreadId}): number[{j}]: {numbers[j]}"); // // // // thread-safe // // option 1: lock // lock (numbers) // { // int oldValue = numbers[j]; // int newValue = oldValue + random.Next(1, 10); // numbers[j] = newValue; // // Console.WriteLine($"Between ({Thread.CurrentThread.ManagedThreadId}): number[{j}]: {numbers[j]}"); // // Thread.Sleep(random.Next(0, 1000)); // // Console.WriteLine($"After ({Thread.CurrentThread.ManagedThreadId}): number[{j}]: {numbers[j]}"); // } // // // option 2: use concurrent collection // // https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/ // // // option 3: use chunks to have no overlapping // // e.g. using Parallel.For or create own chunks // } // } // ); // } // // Dictionary dictionary = new Dictionary(); // for (int i = 0; i < tasks.Length; i++) // { // tasks[i] = Task.Run(() => // { // lock (dictionary) // lock is one option, the other is to use concurrent collection (here ConcurrentDictionary) // { // int key = random.Next(); // if (!dictionary.ContainsKey(key)) // { // // dictionary[key] = $"Thread ({Thread.CurrentThread.ManagedThreadId}) number({key}): {random.Next()}"; // dictionary.TryAdd(key, $"Thread ({Thread.CurrentThread.ManagedThreadId}) number({key}): {random.Next()}"); // } // } // }); // } // ConcurrentDictionary concurrentDictionary = new ConcurrentDictionary(); // for (int i = 0; i < tasks.Length; i++) // { // tasks[i] = Task.Run(() => // { // int key = random.Next(); // if (!concurrentDictionary.ContainsKey(key)) // { // // dictionary[key] = $"Thread ({Thread.CurrentThread.ManagedThreadId}) number({key}): {random.Next()}"; // concurrentDictionary.TryAdd(key, // $"Thread ({Thread.CurrentThread.ManagedThreadId}) number({key}): {random.Next()}"); // } // }); // } File.Delete("output.txt"); object fileLock = new object(); for (int i = 0; i < tasks.Length; i++) { tasks[i] = Task.Run(() => { // not thread safe // File.AppendAllText("output.txt", $"Thread ({Thread.CurrentThread.ManagedThreadId}) number: {random.Next()}\n"); // FileStream fs = File.OpenWrite("output.txt"); // FileStream fs = File.Open("output.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite); // string s = $"Thread ({Thread.CurrentThread.ManagedThreadId}) number: {random.Next()}\n"; // fs.Write(Encoding.UTF8.GetBytes(s), 0, s.Length); // fs.Close(); // fs.Dispose(); // thread safe lock (fileLock) { FileStream fs = File.Open("output.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite); string s = $"Thread ({Thread.CurrentThread.ManagedThreadId}) number: {random.Next()}\n"; fs.Write(Encoding.UTF8.GetBytes(s), 0, s.Length); fs.Close(); fs.Dispose(); } }); } Task.WaitAll(tasks); Console.WriteLine("----- Program ended -----");