Added: new projects for Asyncs and RaceConditions with implementations of task handling, async programming, and thread safety.

This commit is contained in:
2026-10-14 18:27:04 +02:00
parent 32d95221e6
commit 39a6de6d85
6 changed files with 316 additions and 7 deletions
+121
View File
@@ -0,0 +1,121 @@
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<int, string> dictionary = new Dictionary<int, string>();
// 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<int, string> concurrentDictionary = new ConcurrentDictionary<int, string>();
// 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 -----");