From df5e59e4bf96315ee44d01867fd267dc23ac99b8 Mon Sep 17 00:00:00 2001 From: David Hackbarth Date: Mon, 30 Jun 2025 18:26:56 +0200 Subject: [PATCH] add multithreading project --- Multithreading/.gitignore | 133 ++++++++++++++++++ .../.idea.Multithreading/.idea/.gitignore | 8 ++ .../.idea.Multithreading/.idea/encodings.xml | 4 + .../.idea/indexLayout.xml | 8 ++ .../.idea/.idea.Multithreading/.idea/vcs.xml | 6 + Multithreading/Multithreading.sln | 16 +++ .../Multithreading/Multithreading.csproj | 10 ++ Multithreading/Multithreading/Tasks.cs | 127 +++++++++++++++++ Multithreading/Multithreading/Threads.cs | 104 ++++++++++++++ 9 files changed, 416 insertions(+) create mode 100644 Multithreading/.gitignore create mode 100644 Multithreading/.idea/.idea.Multithreading/.idea/.gitignore create mode 100644 Multithreading/.idea/.idea.Multithreading/.idea/encodings.xml create mode 100644 Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml create mode 100644 Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml create mode 100644 Multithreading/Multithreading.sln create mode 100644 Multithreading/Multithreading/Multithreading.csproj create mode 100644 Multithreading/Multithreading/Tasks.cs create mode 100644 Multithreading/Multithreading/Threads.cs diff --git a/Multithreading/.gitignore b/Multithreading/.gitignore new file mode 100644 index 0000000..8c7f0e8 --- /dev/null +++ b/Multithreading/.gitignore @@ -0,0 +1,133 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.svclog +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml +*.azurePubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +packages/ +## TODO: If the tool you use requires repositories.config, also uncomment the next line +!packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +![Ss]tyle[Cc]op.targets +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml + +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +# ========================= +# Windows detritus +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac desktop service store files +.DS_Store + +_NCrunch* diff --git a/Multithreading/.idea/.idea.Multithreading/.idea/.gitignore b/Multithreading/.idea/.idea.Multithreading/.idea/.gitignore new file mode 100644 index 0000000..0bb2234 --- /dev/null +++ b/Multithreading/.idea/.idea.Multithreading/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/projectSettingsUpdater.xml +/.idea.Multithreading.iml +/contentModel.xml diff --git a/Multithreading/.idea/.idea.Multithreading/.idea/encodings.xml b/Multithreading/.idea/.idea.Multithreading/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/Multithreading/.idea/.idea.Multithreading/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml b/Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/Multithreading/.idea/.idea.Multithreading/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml b/Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/Multithreading/.idea/.idea.Multithreading/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Multithreading/Multithreading.sln b/Multithreading/Multithreading.sln new file mode 100644 index 0000000..bc7231f --- /dev/null +++ b/Multithreading/Multithreading.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multithreading", "Multithreading\Multithreading.csproj", "{03A1EA82-F21F-45B0-A6D2-3275164FD3E9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03A1EA82-F21F-45B0-A6D2-3275164FD3E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {03A1EA82-F21F-45B0-A6D2-3275164FD3E9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {03A1EA82-F21F-45B0-A6D2-3275164FD3E9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {03A1EA82-F21F-45B0-A6D2-3275164FD3E9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Multithreading/Multithreading/Multithreading.csproj b/Multithreading/Multithreading/Multithreading.csproj new file mode 100644 index 0000000..945fa72 --- /dev/null +++ b/Multithreading/Multithreading/Multithreading.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + disable + enable + + + diff --git a/Multithreading/Multithreading/Tasks.cs b/Multithreading/Multithreading/Tasks.cs new file mode 100644 index 0000000..055a031 --- /dev/null +++ b/Multithreading/Multithreading/Tasks.cs @@ -0,0 +1,127 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Multithreading +{ + internal class Program + { + public static void Main(string[] args) + { + Console.WriteLine("Program started"); + + // Task task = new Task(() => + // { + // for (int i = 0; i < 10; i++) + // { + // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}"); + // Thread.Sleep(500); + // } + // }); + // task.Start(); + // + // Task task2 = Task.Run(() => + // { + // for (int i = 0; i < 10; i++) + // { + // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}"); + // Thread.Sleep(500); + // } + // }); + // + // task.Wait(); + // task2.Wait(); + // + // Task task3 = new Task(() => + // { + // for (int i = 0; i < 10; i++) + // { + // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}"); + // Thread.Sleep(500); + // } + // }); + // task3.RunSynchronously(); + + // CancellationTokenSource cts = new CancellationTokenSource(); + // CancellationToken token = cts.Token; + // + // Task task4 = Task.Run(() => + // { + // for (int i = 0; i < 10; i++) + // { + // Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {i}"); + // Thread.Sleep(500); + // + // // token.ThrowIfCancellationRequested(); + // if (token.IsCancellationRequested) + // { + // // throw new TaskCanceledException(); + // break; + // } + // } + // }, token); + // + // ConsoleKeyInfo keyInfo = Console.ReadKey(); + // if (keyInfo.Key == ConsoleKey.A) cts.Cancel(); + // + // try + // { + // task4.Wait(token); + // } + // catch (OperationCanceledException e) + // { + // Console.WriteLine(e); + // } + // + // cts.Dispose(); + + // CancellationTokenSource cts2 = new CancellationTokenSource(); + Random random = new Random(); + Task[] tasks = new Task[10]; + for (int i = 0; i < tasks.Length; i++) + { + tasks[i] = Task.Run(() => + { + Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}) started"); + for (int j = 0; j < 10; j++) + { + Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}): {j}"); + Thread.Sleep(random.Next(100, 1000)); + + if (random.NextSingle() > 0.9f) + { + Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}) aborted"); + // cts2.Cancel(); + throw new OperationCanceledException(); + } + } + + Console.WriteLine($"Task ({Thread.CurrentThread.ManagedThreadId}) ended"); + }); + Console.WriteLine($"Task with id {tasks[i].Id} created"); + Console.WriteLine($"Task ({tasks[i].Id}): {tasks[i].Status}"); + } + + try + { + Task.WaitAll(tasks); + // Task.WaitAny(tasks); + } + catch (OperationCanceledException) + { + Console.WriteLine("Operation was canceled"); + } + catch (AggregateException) + { + Console.WriteLine("One or more tasks canceled"); + } + + for (int i = 0; i < tasks.Length; i++) + { + Console.WriteLine($"Task ({tasks[i].Id}): {tasks[i].Status}"); + } + + Console.WriteLine("Program ended"); + } + } +} \ No newline at end of file diff --git a/Multithreading/Multithreading/Threads.cs b/Multithreading/Multithreading/Threads.cs new file mode 100644 index 0000000..a93394a --- /dev/null +++ b/Multithreading/Multithreading/Threads.cs @@ -0,0 +1,104 @@ +// using System; +// using System.Threading; +// +// namespace Multithreading +// { +// internal class Program +// { +// private static bool _shouldExit; +// +// public static void Main(string[] args) +// { +// Console.WriteLine("Program started"); +// +// // Console.WriteLine($"{Thread.CurrentThread.Name}"); +// // Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}"); +// // +// // Console.WriteLine("Before Sleep"); +// // Thread.Sleep(10000); +// // Console.WriteLine("After Sleep"); +// +// // Console.ReadKey(); +// +// // Thread thread = new Thread(() => +// // { +// // for (int i = 0; i < 10; i++) +// // { +// // Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}): {i}"); +// // Thread.Sleep(500); +// // } +// // }); +// // thread.IsBackground = true; +// // thread.Start(); +// // +// // Thread[] threads = new Thread[10]; +// // for (int i = 0; i < threads.Length; i++) +// // { +// // threads[i] = new Thread(PrintAndSleep); +// // // threads[i].IsBackground = true; +// // threads[i].Start(); +// // } +// +// // for (int i = 0; i < 10; i++) +// // { +// // Console.WriteLine($"Main: {i}"); +// // Thread.Sleep(250); +// // } +// // +// // thread.Join(); +// // Console.WriteLine($"Thread {thread.ManagedThreadId} is finished with {thread.ThreadState}"); +// // +// // // thread.Start(); // CAUTION: you can not restart a thread +// // +// // for (int i = 0; i < threads.Length; i++) +// // { +// // threads[i].Join(5000); +// // Console.WriteLine($"Thread {threads[i].ManagedThreadId} is finished with {threads[i].ThreadState}"); +// // // _shouldExit = true; +// // // Console.WriteLine($"Thread {threads[i].ManagedThreadId} is finished with {threads[i].ThreadState}"); +// // // threads[i].Join(5000); +// // // Console.WriteLine($"Thread {threads[i].ManagedThreadId} is finished with {threads[i].ThreadState}"); +// // } +// // +// // for (int i = 0; i < threads.Length; i++) +// // { +// // threads[i] = new Thread(PrintAndSleep); +// // threads[i].Start(); +// // } +// +// Console.WriteLine("------------ THREADPOOL ------------"); +// +// // ThreadPool.SetMaxThreads(1, 1); +// +// for (int i = 0; i < 10; i++) +// { +// ThreadPool.QueueUserWorkItem(_ => PrintAndSleep()); +// } +// +// Thread.Sleep(5000); +// +// for (int i = 0; i < 10; i++) +// { +// ThreadPool.QueueUserWorkItem(_ => PrintAndSleep()); +// } +// +// Thread.Sleep(5000); +// +// Console.WriteLine("Program ended"); +// } +// +// private static void PrintAndSleep() +// { +// Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}) started"); +// for (int i = 0; i < 5; i++) +// { +// if (_shouldExit) return; +// +// Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}): {i}"); +// Thread.Sleep(500); +// } +// Console.WriteLine($"Thread ({Thread.CurrentThread.ManagedThreadId}) ended"); +// } +// } +// } +//