Today I want to talk about threads since every programmer works with them and absolutely loves them. Typically, when we create a thread, your code can look like this:
static void Main(string[] args)
{
TraditionalThreadCreation();
Console.Read();
}
static void TraditionalThreadCreation()
{
Thread thread1 = new Thread(Task);
Thread thread2 = new Thread(Task);
thread1.Start(0);
thread2.Start(1);
thread2.Join();
}
static void Task(object p)
{
for (int i = int.Parse(p.ToString()); i <=10; i += 2)
Console.WriteLine(i);
}
There is nothing wrong with creating threads like the code above. But there is one thing you have to keep in mind. Thread creation and startup has overhead. A typical thread can take up 1MB of your precious memory. Shocked?? Well, you are not alone.
To mitigate this overhead, the .NET Framework has given you the thread pool (No, this is not a swimming pool for threads). Basically, these are pre-created threads that are shared and recycled. They save you the trouble you have creating threads but most importantly they alleviate the overhead in creating a thread.
Thread Pool - Things you should know:
- They run in the background.
- They are limited and when you’ve used them up; your tasks will begin to queue up.
- The Thread Pool only runs a certain amount of threads simultaneously. This is done to not choke your CPU.
How Can You Use the Thread Pool You Ask?
Well, that is done in a few ways. But for now, I will list 2 that are pre .NET Framework 4.0:
ThreadPool.QueueUserWorkItem
BackgroundWorker
Thread Pool
This is very easy to use. Here is how.
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(Task, 1);
}
static void Task(object p)
{
for (int i = int.Parse(p.ToString()); i <=10; i += 2)
Console.WriteLine(i);
}
Now how much faster is the Thread Pool vs Traditional Thread execution? Well, it depends. Remember that your task may become queued if the limit on Thread Pool has been reached or the total number of simultaneous threads have been met.
Just for kicks, I tried just starting and executing s single thread traditionally and via the thread pool and on average, the thread pool was 200% faster.
class Program
{
static void Main(string[] args)
{
TraditionalThreadCreation();
Thread.Sleep(1000);
ThreadPoolThreadCreation();
Console.Read();
}
private static void ThreadPoolThreadCreation()
{
var start = DateTime.Now;
ThreadPool.QueueUserWorkItem(Task, new object[]
{start, "ThreadPoolThreadCreation() Finished: {0}" });
}
private static void TraditionalThreadCreation()
{
var start = DateTime.Now;
Thread thread = new Thread(Task);
thread.Start(new object[] {start,"TraditionalThreadCreation() Finished: {0}" });
}
private static void Task(object p)
{
var start = (DateTime)((object[])p)[0];
var end = DateTime.Now;
var message = ((object[])p)[1].ToString();
Console.WriteLine(message, end - start);
for (int i = 0; i <= 10; i += 2)
Console.Write(i);
Console.WriteLine();
}
}
Background Worker
This guy is in the System.ComponentModel
namespace. It is used primarily in GUI development where you execute a task on background thread and periodically update GUI elements on the foreground thread in the form of progress reporting. To use BackgroundWorker
, you have to do a few things.
- Instantiate
- Handle the
DoWork
Event - Call
RunWorkerAsync
class Program
{
private static BackgroundWorker _theBGW = new BackgroundWorker();
static void Main(string[] args)
{
BackgroundWorkerRun();
Console.Read();
}
static void BackgroundWorkerRun()
{
_theBGW.DoWork += new DoWorkEventHandler(bw_DoWork);
_theBGW.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
_theBGW.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
_theBGW.WorkerSupportsCancellation = true;
_theBGW.WorkerReportsProgress = true;
_theBGW.RunWorkerAsync();
Thread.Sleep(1000);
}
static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(!e.Cancelled)
Console.WriteLine("Worker Done!! - {0}", e.Result);
else
Console.WriteLine("The answer to the universe will remain unknown");
}
static void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine("Completed {0}%", e.ProgressPercentage);
}
static void bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 10; i <= 100; i += 10)
{
if (!_theBGW.CancellationPending)
{
_theBGW.ReportProgress(i);
Thread.Sleep(1000);
}
else
{
e.Cancel = true;
return;
}
}
e.Result = "The secret to the universe is 42";
}
}
In my next post, I will talk about the TPL (Task Parallel Library) in .NET Framework 4.0.
CodeProject
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.