Threads Suck, Just Abstract It!

One of my favorite quotes is by David Wheeler:

Any problem in computer science can be solved with another layer of indirection.

While this quote is certainly true, problems can arise when the layer of abstraction becomes too heavy. In .NET 2.0 the ThreadPool was introduced and it gave developers a very heavy abstraction over threads. It optimized the execution of multiple threads, but at the same time the developer was abstracted away from the individual threads so much that they lost a significant amount of control.

In .NET 4.0 the Parallel Extensions to .NET are now integrated into the core framework. This means that in the next version of Visual Studio we are going to get all of the new parallel programming features by default. One of these new features is the Task Parallel Library. This feature is a set of classes, called Tasks, which allow us to fire off actions asynchronously.

It looks something like this:

Task task = Task.Create(n => LongRunningMethod(test));
task.Start();
task.Wait();

You might be looking at that and saying to yourself, “well, they just reinvented threads and called them tasks”. Well, that isn’t really the case. For example, the following two chunks of code are quite different:

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *