Parallel Tasks - .net 4.0 feature

I hit upon a class called ‘Parallel’ in the ‘System.Threading.Tasks’ namespace and found it worth a mention on my blog.

This blog is on the same lines as the Parallel LINQ – PLINQ article I wrote last month – performing tasks in parallel to make the entire activity more efficient. But there are some differences. Let’s just get to know this class first.

So, here’s my requirement – pull the ‘title’ element of a bunch of websites. The normal way would be to loop through the list of url’s in a for loop (recall that this will be done synchronously). Of course, we’re doing it another way, if not this article would not have existed.

Following is my set up:

   1: private static void ReadTitleFromUrl(string url)
   2: {
   3:     WebClient webClient = new WebClient();
   4:     string downloadString = webClient.DownloadString(url);
   5:  
   6:     Console.WriteLine("Url: {0}\nTitle: {1}\n", url, GetTitle(downloadString));
   7: }
   8:  
   9: private static string GetTitle(string xmlString)
  10: {
  11:     string title =
  12:         xmlString.Substring(xmlString.IndexOf("<title>") + 7);
  13:     title = title.Substring(0, title.IndexOf("</title>")).Replace("\n", "").Replace("\r", "").Replace("\t", "").Trim();
  14:     return title;
  15: }

The ReadTitleFromUrl method is the heart of the operations - download the web request as a string, parse the title and add display the details. Just a note on the ‘messy’ GetTitle method, I had to do it this way because the returned download string was not compliant to XML rules (so could not load it into any kind of xml readers). Now comes the ‘magic’ method.

   1: private static void UseParallel()
   2: {
   3:     List<string> urls = new List<string>
   4:                             {
   5:                                 "http://www.arunmahendrakar.com/",
   6:                                 "http://weblogs.asp.net/nmarun/archive/2010/06/07/readonly-keyword.aspx",
   7:                                 "http://weblogs.asp.net/nmarun/archive/2010/07/30/mvc3-webformviewengine-or-cshtmlviewengine.aspx",
   8:                                 "http://weblogs.asp.net/nmarun/archive/2010/05/05/using-unity-application-block-from-basics-to-generics.aspx"
   9:                             };
  10:  
  11:     Parallel.ForEach(urls, url => ReadTitleFromUrl(url));
  12: }

Read the method as: For each of the url in the list, run the ReadTitleFromUrl() method, in-parallel. Now, one thing to note is that since these tasks are done in parallel, there’s no guarantee for the order of the return values. This is similar to the behavior of PLINQ resultset.

On this run, I get the following output on my Core 2 Duo.

   1: Url: http://www.arunmahendrakar.com/
   2: Title: Photography and Eye
   3:  
   4: Url: http://weblogs.asp.net/nmarun/archive/2010/07/30/mvc3-webformviewengine-or-cshtmlviewengine.aspx
   5: Title: MVC3 WebFormViewEngine or CshtmlViewEngine - IBloggable - implemented
   6:  
   7: Url: http://weblogs.asp.net/nmarun/archive/2010/06/07/readonly-keyword.aspx
   8: Title: readonly keyword - IBloggable - implemented
   9:  
  10: Url: http://weblogs.asp.net/nmarun/archive/2010/05/05/using-unity-application-block-from-basics-to-generics.aspx
  11: Title: Using Unity Application Block ƒ?" from basics to generics - IBloggable - implemented

You see the order is a little messed up.

As for the timing issues, the average for Parallel tasks came up to be 3880ms and while using a ‘for’ loop, I get 5957ms. We clearly have a winner.

Digging more detail on this, I found out there’s something called Task Parallel Library dedicated to allow applications be more productive by making them work in parallel. TPL makes use of all the processes available on your machine. It handles the partitioning of the activity into smaller tasks, state management of the threads and other inner details. TPL is suited for both I/O bound operations as well as CPU bound partitions of work.

So then how is PLINQ different? You can control the number of threads in PLINQ, whereas the Parallel class manages this for you. Also, when you have an activity that is mix of I/O and CPU bound operation, Parallel seems to be the choice.

The code used in the blog can be downloaded from here.

Verdict:

1. ‘Parallel’ is always faster – false
2. Not all looping is suitable for parallel processing
3. Parallelism adds complexity to your application
4. Larger the size of the activity, greater the benefit of parallel processing

1 Comment

Comments have been disabled for this content.