Prior to working on a MonoDroid project for my employer, I had near-zero experience with multi-threaded programming. On mobile devices, if you hang the UI up for more than a few seconds (varies by platform), the OS will tell the user your app is unresponsive and offer to close it. Thus, it becomes very important to move potentially-long-running process out of the UI thread and I have been forced to dive into multi-threading whether I want to or not :)

Earlier this year, before I started working on and learning about Android, I had the privilege of attending TechEd 2011. I went to a session about the Task Parallel Library and the new Async CTP but I had no idea what TPL, CTP, etc even were. So, the session was interesting but hard to remember or understand without a frame of reference.

Last night our local programming social group had a speaker up to talk about the new Async stuff coming in the next version of .NET and available as a CTP here. I’m not going to go into detail on how to actually use these as there are tons of resources out there on that subject. Instead I’m just sharing my main takeaway, which was why do I use Async instead of TPL and what the strengths are with both systems:

The Task Parallel Library (and multi-threading models in general) abstracts multi-threading and simplifies complex threading and synchronization problems. The downside of this is that spinning up threads to handle different processes consumes extra resources. It makes sense to tap these resources if you have multiple things going on that are computationally heavy. Tasks like network requests are not cpu-intensive but take time due to network latency. Since freezing the UI thread to wait for a network response is not desireable, many times these network requests are offloaded on separate threads. But this ties up extra resources just to wait on a web service.

Enter the new Async stuff. As I understand it, Async allows the programmer to make asynchronous calls without tying up more threads. It provides the two main keywords: Async and Await that allow you to make a network request and attach a callback and continue running without tying up the UI thread and without spinning off multiple threads.

So, Task Parallel Library is great for computationally-heavy tasks that need to be accomplished outside of the main program loop. It takes advantage of multi-threading and multiple cores.

Async is great for non-computationally-heavy tasks, such as network requests, because it cuts out the [unnecessary] overhead of multi-threading. It does not tie up multiple threads or cores.

Please leave a comment if I have any incorrect thoughts here – I readily admit to being an async/multi-threading newbie!