real world programming

Multi Threading

Posted by kevinup on December 10, 2008

A little something that I’ve found myself doing a lot is performance tuning. It’s something that I find myself being delegated with, and something I really enjoy.

When performance tuning one technique is to look for places where there is work being done sequentially that could be getting done in parallel. When found, I usually start writing the same class to use the .net framework to parallelize it. Its time to make that code available for anyone else that wants to use it, plus I’ll have this post so I can start to have a repository for my code.

The idea is you have a list of inputs, do work on it, and get back a list of outputs. In the middle you have some worker class that takes a single input, and returns a single output. For the sake of an example, let’s say I have a list of urls, and I need to hit a couple websites, and return their HTML. The syntax would look like this:

List<string> returnedHtml = ThreadManager.ExecuteParallel<HitWebpage, string, string>(inputUrls);

ThreadManager spools everything up, and does the parallelizing. The first generic in ExecuteParallel is the class that does the actual work. The second represents a list of that type of inputs, in this case, a List of string’s. The last generic represents a list of the output, in this case also a list of string’s.  When using this method of parallelizing, all you have to do is write a worker class. It needs to inherit from ThreadBase, and you just need to implement Execute. Here’s what my HitWebpage class looks like:

    public class HitWebpage : ThreadBase<string, string>


        public override string Execute(string input)


            WebRequest request = WebRequest.Create(input);

            WebResponse response = request.GetResponse();

            StreamReader reader = new StreamReader(response.GetResponseStream());

            return reader.ReadToEnd();




I’m hoping this looks pretty simple. I’m putting the rest of the code here on if you’re interested.


2 Responses to “Multi Threading”

  1. Erik said

    That’s pretty good to know. I didn’t know it’s so easy. There’s an error in your post though. It says instead of 🙂

  2. kevinup said

    True, good catch.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: