{"id":204,"date":"2024-05-19T03:02:07","date_gmt":"2024-05-19T06:02:07","guid":{"rendered":"https:\/\/eliasdc.dev\/?p=204"},"modified":"2024-09-08T09:38:44","modified_gmt":"2024-09-08T12:38:44","slug":"threads-is-better-use-it-or-not","status":"publish","type":"post","link":"https:\/\/eliasdc.dev\/index.php\/2024\/05\/19\/204\/","title":{"rendered":"Threads: Is Better Use it or Not?"},"content":{"rendered":"\n<p>In the world of .NET development, threads are powerful tools that enable the simultaneous execution of multiple tasks. However, deciding whether or not to use them in your project can be a challenge. To help you make this informed decision, this post will explore the pros and cons of using threads, providing you with a comprehensive overview of the landscape. Additionally, we\u2019ll delve into a practical example to illustrate the comportament and the performance benefits of threads firsthand.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"225\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/popcorn-2.gif\" alt=\"\" class=\"wp-image-216\"\/><\/figure>\n\n\n\n<p class=\"has-large-font-size\"><strong>Advantages of Using Threads<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>Increased Application Responsiveness:<\/strong> By distributing tasks among threads, it is possible to keep the application&#8217;s interface responsive even when time-consuming operations are in progress. <\/li>\n\n\n\n<li><strong>Better Use of Computer Resources:<\/strong> Threads allow you to harness the full potential of modern computers with multicore processors. By dividing processing between different cores, you can achieve significant performance boosts, especially for computationally intensive tasks.<\/li>\n\n\n\n<li><strong>Simplified Asynchronous Development:<\/strong> Threads facilitate the implementation of asynchronous code, which has become increasingly important for handling I\/O operations and other tasks that can block the main thread.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-large-font-size\"><strong>Disadvantages of Using Threads<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"320\" height=\"240\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/Afraid-Adult.gif\" alt=\"\" class=\"wp-image-213\"\/><\/figure>\n\n\n\n<ul>\n<li><strong>Increased Code Complexity:<\/strong> Managing threads effectively requires careful consideration to avoid issues like deadlocks and race conditions. These complexities can increase development time and make code maintenance more challenging. It&#8217;s crucial to have a solid understanding of multithreading concepts and best practices to ensure code stability and reliability.<\/li>\n\n\n\n<li><strong>Potential for Performance Problems:<\/strong> If not used cautiously, threads can introduce performance bottlenecks, such as lock contention and context overhead. These issues are particularly prevalent in systems with numerous concurrent threads.<\/li>\n\n\n\n<li><strong>Debugging Difficulty:<\/strong> Debugging problems in multithreaded code can be extremely challenging due to the asynchronous and interdependent nature of threads.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-large-font-size\"><strong>Additional Considerations<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>The Type of Application:<\/strong> The use of threads is particularly advantageous for applications that need to perform time-consuming background operations or maintain a responsive interface during these operations.<\/li>\n\n\n\n<li><strong>Developer Experience:<\/strong> If you&#8217;re new to threads, it&#8217;s advisable to start with simple projects before venturing into complex applications. <\/li>\n\n\n\n<li><strong>Available Tools:<\/strong> .NET offers a comprehensive set of tools and libraries to streamline multithreaded development, including <code>Task<\/code> and <code>async\/await<\/code>. These tools simplify code, reduce the likelihood of errors, and enhance overall development productivity. <\/li>\n<\/ul>\n\n\n\n<p class=\"has-large-font-size\"><strong>Practical Example<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"320\" height=\"320\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/Wakeup.gif\" alt=\"\" class=\"wp-image-214\"\/><\/figure>\n\n\n\n<p>To solidify our understanding of the concepts discussed, let&#8217;s explore a practical example that demonstrates the difference between a simple method executed with and without threads. The complete project code is available in the <\/p>\n\n\n\n<p style=\"font-size:25px\"><strong>GitHub repository: <a href=\"https:\/\/github.com\/eliasedc\/ThreadsSolution\" target=\"_blank\" rel=\"noopener\" title=\"\">https:\/\/github.com\/eliasedc\/ThreadsSolution<\/a>.<\/strong><\/p>\n\n\n\n<p>In this project, we&#8217;ll examine two methods:<\/p>\n\n\n\n<ul>\n<li><strong>MethodWithoutThreads(): <\/strong>Executes the ForTest() method without threads.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * 1.125rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">C#<\/span><span role=\"button\" tabindex=\"0\" data-code=\"async Task MethodWithoutThreads(int pNumber)\n{\n    \/\/If there is an await inside the ForTest method, the code within it will free up resources to go to the next one (it will run asynchronously).\n    \/\/If not, the code will run as if it were synchronized.\n    Task t1 = ForTest(&quot;ELIASDC&quot;, pNumber);\n    Task t2 = ForTest(&quot;DEV&quot;, pNumber);\n\n    await Task.WhenAll(t1, t2); \/\/Useful if within the methods above there is a process with await\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">async<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Task<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">MethodWithoutThreads<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">pNumber<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/If there is an await inside the ForTest method, the code within it will free up resources to go to the next one (it will run asynchronously).<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/If not, the code will run as if it were synchronized.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #4EC9B0\">Task<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">t1<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">ForTest<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;ELIASDC&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">pNumber<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #4EC9B0\">Task<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">t2<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #DCDCAA\">ForTest<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;DEV&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">pNumber<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">await<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">Task<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">WhenAll<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">t1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">t2<\/span><span style=\"color: #D4D4D4\">); <\/span><span style=\"color: #6A9955\">\/\/Useful if within the methods above there is a process with await<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ul>\n<li><strong>MethodWithThreads()<\/strong><em>: <\/em>Executes the ForTest() method on threads.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * 1.125rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">C#<\/span><span role=\"button\" tabindex=\"0\" data-code=\"async Task MethodWithThreads(int pNumber)\n{\n    Task t1 = Task.Run(() =&gt; ForTest(&quot;ELIASDC&quot;, pNumber));\n    Task t2 = Task.Run(() =&gt; ForTest(&quot;DEV&quot;, pNumber));\n\n    await Task.WhenAll(t1, t2);\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">async<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Task<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">MethodWithThreads<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">pNumber<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #4EC9B0\">Task<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">t1<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">Task<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">Run<\/span><span style=\"color: #D4D4D4\">(() =&gt; <\/span><span style=\"color: #DCDCAA\">ForTest<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;ELIASDC&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">pNumber<\/span><span style=\"color: #D4D4D4\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #4EC9B0\">Task<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">t2<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">Task<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">Run<\/span><span style=\"color: #D4D4D4\">(() =&gt; <\/span><span style=\"color: #DCDCAA\">ForTest<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;DEV&quot;<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">pNumber<\/span><span style=\"color: #D4D4D4\">));<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">await<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">Task<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">WhenAll<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">t1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">t2<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>When running the application, you&#8217;ll observe that MethodWithThreads() writes to the console simultaneously, unlike MethodWithoutThreads():<\/p>\n\n\n\n<p style=\"font-size:26px\"><strong>MethodWithoutThreads<\/strong>&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"416\" height=\"508\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/MethodWithoutThreads.png\" alt=\"\" class=\"wp-image-230\" srcset=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/MethodWithoutThreads.png 416w, https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/MethodWithoutThreads-246x300.png 246w\" sizes=\"(max-width: 416px) 100vw, 416px\" \/><\/figure>\n\n\n\n<p style=\"font-size:26px\"><strong>MethodWithThreads<\/strong>&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"414\" height=\"780\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/MethodWithThreads.png\" alt=\"\" class=\"wp-image-231\" srcset=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/MethodWithThreads.png 414w, https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/MethodWithThreads-159x300.png 159w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/figure>\n\n\n\n<p class=\"has-large-font-size\"><strong>Pratical Example BONUS 1: A little more about &#8220;Task&#8221;<\/strong><\/p>\n\n\n\n<p>This isn&#8217;t the focus of the post, but the provided code includes commented examples of thread-related functions in the &#8220;MyWriteLine&#8221; method: <code>await Task.Delay<\/code>, <code>Thread.Sleep<\/code>, and <code>Thread.Delay(x).Wait()<\/code>. Uncommenting these lines allows developers to explore the nuances and performance implications of each method.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * 1.125rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">C#<\/span><span role=\"button\" tabindex=\"0\" data-code=\"async Task MyWriteLine(string pText)\n{\n    Console.WriteLine(pText);\n    \/\/\/\/This create a new Task with delay(x) and release the resource from thread to run other code lines\n    \/\/\/\/If you uncomment, you can see that even using &quot;MethodWithoutTrhreads&quot; the &quot;ForTest&quot; method will run asynchronously. \n    \/\/\/\/This works due to the use of async\/await.\n    \/\/\/\/FYI: if you do it, I suggest set the &quot;intNumberToFor&quot; to 1000 or less;\n    \/\/await Task.Delay(1); \n\n    \/\/\/\/Suspends the current thread. Does't release resource from thread to run other code lines\n    \/\/Thread.Sleep(1); \n\n    \/\/\/\/Waits for the Delay(x) to complete execution, in the practic is similar to Thread.Sleep\n    \/\/Task.Delay(1000).Wait(); \n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">async<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Task<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">MyWriteLine<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">string<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">pText<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">Console<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">WriteLine<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">pText<\/span><span style=\"color: #D4D4D4\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/\/\/This create a new Task with delay(x) and release the resource from thread to run other code lines<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/\/\/If you uncomment, you can see that even using &quot;MethodWithoutTrhreads&quot; the &quot;ForTest&quot; method will run asynchronously. <\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/\/\/This works due to the use of async\/await.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/\/\/FYI: if you do it, I suggest set the &quot;intNumberToFor&quot; to 1000 or less;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/await Task.Delay(1); <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/\/\/Suspends the current thread. Does&#39;t release resource from thread to run other code lines<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/Thread.Sleep(1); <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/\/\/Waits for the Delay(x) to complete execution, in the practic is similar to Thread.Sleep<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">    \/\/Task.Delay(1000).Wait(); <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>Pratical Example BONUS 2 : Benchmarking <\/strong><\/p>\n\n\n\n<p>To evaluate the performance difference between the existing <code class=\"\">MethodWithoutThreads<\/code> and <code class=\"\">MethodWithThreads<\/code> approaches, we conducted a benchmark. The results, as illustrated in the accompanying screenshot<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"157\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/benchmarck-Threads.png\" alt=\"\" class=\"wp-image-229\" srcset=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/benchmarck-Threads.png 782w, https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/benchmarck-Threads-300x60.png 300w, https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/benchmarck-Threads-768x154.png 768w\" sizes=\"(max-width: 782px) 100vw, 782px\" \/><\/figure>\n\n\n\n<p>The benchmark demonstrates an interesting trend regarding thread utilization. For smaller for loop iterations, the single-threaded approach (MethodWithoutThreads) exhibits slightly better performance. However, as the loop size increases, the multithreaded approach (MethodWithThreads) consistently outperforms the single-threaded method. This behavior can be attributed to the overhead associated with thread creation and management, which becomes less significant as the workload grows.<\/p>\n\n\n\n<p><strong>The code for this benchmark is also included in the same solution on GitHub.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"276\" height=\"208\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/Agree.gif\" alt=\"\" class=\"wp-image-215\"\/><\/figure>\n\n\n\n<p class=\"has-large-font-size\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>The decision to utilize threads hinges on a multitude of factors, including the type of application, developer expertise, and available tools. It&#8217;s crucial to meticulously evaluate the pros and cons before making a decision. If you opt to employ threads, ensure you familiarize yourself with multithreaded development best practices to avert performance and debugging issues.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>That&#8217;s it<\/strong><\/p>\n\n\n\n<p>I hope this post and the code serve as your compass to navigate the universe of threads in .NET<\/p>\n\n\n\n<p>See you next time!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"237\" src=\"https:\/\/eliasdc.dev\/wp-content\/uploads\/2024\/05\/clabs.gif\" alt=\"\" class=\"wp-image-217\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the world of .NET development, threads are powerful tools that enable the simultaneous execution of multiple tasks. However, deciding whether or not to use them in your project can be a challenge. To help you make this informed decision, this post will explore the pros and cons of using threads, providing you with a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":235,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1],"tags":[16,14,12,10,13,15,11],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/posts\/204"}],"collection":[{"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":19,"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":234,"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/posts\/204\/revisions\/234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/media\/235"}],"wp:attachment":[{"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eliasdc.dev\/index.php\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}