Comments on: Difference Between Asynchronous Programming and Multithreading in C# https://code-maze.com/csharp-async-vs-multithreading/ Learn. Code. Succeed. Wed, 10 Aug 2022 10:44:37 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: Mats https://code-maze.com/csharp-async-vs-multithreading/#comment-6278 Wed, 10 Aug 2022 10:44:37 +0000 https://drafts.code-maze.com/?p=61476#comment-6278 In reply to Marinko Spasojević.

Thank you, I think I get it. Async spawns processes more of “on demand” rather than preemptively; i.e. if no blocking operation is encountered then no process is spawned? So a loop with say 100 function calls where only 3 of them get blocked would in essence only use 4 threads instead of 101 if it had been coded as multi-threaded?

]]>
By: Marinko Spasojević https://code-maze.com/csharp-async-vs-multithreading/#comment-6277 Wed, 10 Aug 2022 09:49:56 +0000 https://drafts.code-maze.com/?p=61476#comment-6277 In reply to Mats.

Well, the most important fact here is that with async you can handle multiple requests at the same time, just if any of those requests reach a blocking operation, they don’t block the used thread, instead, the thread is returned for some other request to use it. But once the blocking operation is done a new thread is assigned to the waiting request so it could be finished (that’s why you see thread 1 as an entry and thread 11,18… as output – this is why I said that async is using multiple threads in my previous reply). That’s the main point of async operations.

]]>
By: Mats https://code-maze.com/csharp-async-vs-multithreading/#comment-6276 Wed, 10 Aug 2022 09:16:39 +0000 https://drafts.code-maze.com/?p=61476#comment-6276 In reply to Marinko Spasojević.

Thank you, I was discussing this with a colleague who insisted that async did not use multiple threads, which is one way to interpret articles like this discussing the differences. My takeaway is rather that with async you don’t control the spawned threads you only get their results whereas with multi-threading you keep control the whole time. There are always multiple threads involved, the difference is how many are “yours”. Is that a fair conclusion?

]]>
By: Marinko Spasojević https://code-maze.com/csharp-async-vs-multithreading/#comment-6275 Wed, 10 Aug 2022 08:59:25 +0000 https://drafts.code-maze.com/?p=61476#comment-6275 In reply to Mats.

Hello Mats. Of course, both examples use multiple threads. As you wrote the last sentence, in the first example, with async the initial thread is always returned to the thread pool (when your code is waiting for some other execution), and then after a new thread is assigned to that request to finish the process. That’s why you see thread 1 for all three requests and different thread numbers once these requests are done. So, both examples are multithreaded, just in the first case, the thread is returned to the thread pool and that same thread can be assigned to another request while the first request is waiting for other executions.

]]>
By: Mats https://code-maze.com/csharp-async-vs-multithreading/#comment-6274 Wed, 10 Aug 2022 08:32:26 +0000 https://drafts.code-maze.com/?p=61476#comment-6274 I still don’t get it. In the first example you start with one thread, and end up with three threads. In the second example you start with three threads and end with three threads. How is the first not multi-threaded if you end up with multiple threads in the end? In your conclusion you’re saying “the assigned thread is returned back to a thread pool” implying that there are multiple threads involved?

]]>
By: Victoria https://code-maze.com/csharp-async-vs-multithreading/#comment-5673 Wed, 18 May 2022 18:51:03 +0000 https://drafts.code-maze.com/?p=61476#comment-5673 Thank you for your thorough explanation. This really illuminated the differences between the two for me, which is something I’ve struggled with. Cheers!

]]>
By: Marinko Spasojević https://code-maze.com/csharp-async-vs-multithreading/#comment-5658 Mon, 16 May 2022 10:33:15 +0000 https://drafts.code-maze.com/?p=61476#comment-5658 In reply to GRev.

IMO, the type of application is irrelevant here. The concept is what matters. Also, we talked a bit about synchronization context here: https://code-maze.com/asynchronous-programming-with-async-and-await-in-asp-net-core/

But as you will see, we don’t have it in the ASP.NET Core apps.

]]>
By: GRev https://code-maze.com/csharp-async-vs-multithreading/#comment-5655 Mon, 16 May 2022 07:34:52 +0000 https://drafts.code-maze.com/?p=61476#comment-5655 This only works with console applications. Maybe u should explain SynchronizationContexts. Would be kinda nice ngl.

]]>
By: Marinko Spasojević https://code-maze.com/csharp-async-vs-multithreading/#comment-4861 Mon, 03 Jan 2022 08:43:35 +0000 https://drafts.code-maze.com/?p=61476#comment-4861 In reply to Bac Vo.

Hello Bac Vo. Thanks. Regarding your question, maybe you can find some answers here

]]>
By: Bac Vo https://code-maze.com/csharp-async-vs-multithreading/#comment-4856 Sun, 02 Jan 2022 23:53:21 +0000 https://drafts.code-maze.com/?p=61476#comment-4856 hi there, good article! I am curious about when the Thread in the Multithreading can be released (freed)? In some production cases, we can get an exception like not enough Thread in the Thread pool to complete the operation. So I would like to know the best way to manage it.

]]>