Multithreading has been there in the world of computer programming for years. Although, almost all developers pretend to know what multithreading is, it looks like multithreading requires a little more attention when implementing in the real world. Of course, multithreading is easy to understand when it is just a theory, but the practical business application is found to be difficult especially if the developer is not thorough with the basics of multithreading.
There have been quite a few suggestions by expert programmers that most programmers have overlooked multithreading. As a result, there are more and more errors created in their code, costing thousands of dollars to the businesses and software companies.
This brief discussion is just an introduction for novice programmers in .NET.
What is Multithreading?
Multithreading can be loosely defined as trying to do more than one thing at a time.
A thread is the flow of the running program which follows instructions written in the code. Before the introduction of multithreading, there was only one thread running for processes in operating systems.
Imagine a scenario where you can browse the web through a web browser and while typing a document in Microsoft Word at the same time. In this case, you get two things done using two programs. Apply the same concept for a single process and what you get as the result is what multithreading is.
There are two scenarios that can be applied for multithreading; the threads that do pure calculations and threads that wait for output for further execution. In the first scenario, threads do not have dependencies. They just perform independent calculations where they do not wait for any 3rd party inputs. In this scenario, there is no real sense of using multithreading as it only consumes the processor power in vain as the processor is required to switch between the threads frequently.
The second scenario is where multithreading is used effectively. In this case, the threads wait for some external inputs and the processor can switch to another thread while the previous thread awaits input. This is rather a smarter way of executing programs without wasting computer’s processing power.
Multithreading in .NET
.NET is designed from scratch to support multithreading. There are two main ways of implementing multithreading in .NET; starting threads with ‘ThreadStart delegates’ (also known as the manual method), or using ‘ThreadPool class’.
If a thread is supposed to be running for a longtime, it is recommended to use ‘ThreadStart’ and manually initiate the thread. If the thread is to run for a brief period, then the smartest way is to use the ‘ThreadPool class’.
The Simplest Multithreading Example
Here is one of the most basic multithreaded programs written in C#.
This program creates the following output on my machine
First Thread: 0
Remaining Threads: 0
Remaining Threads: 1
First Thread: 1
Remaining Threads: 2
Remaining Threads: 3
First Thread: 2
Remaining Threads: 4
Remaining Threads: 5
First Thread: 3
Remaining Threads: 6
Remaining Threads: 7
There have been quite a few suggestions by expert programmers that most programmers have overlooked multithreading. As a result, there are more and more errors created in their code, costing thousands of dollars to the businesses and software companies.
This brief discussion is just an introduction for novice programmers in .NET.
What is Multithreading?
Multithreading can be loosely defined as trying to do more than one thing at a time.
A thread is the flow of the running program which follows instructions written in the code. Before the introduction of multithreading, there was only one thread running for processes in operating systems.
Imagine a scenario where you can browse the web through a web browser and while typing a document in Microsoft Word at the same time. In this case, you get two things done using two programs. Apply the same concept for a single process and what you get as the result is what multithreading is.
There are two scenarios that can be applied for multithreading; the threads that do pure calculations and threads that wait for output for further execution. In the first scenario, threads do not have dependencies. They just perform independent calculations where they do not wait for any 3rd party inputs. In this scenario, there is no real sense of using multithreading as it only consumes the processor power in vain as the processor is required to switch between the threads frequently.
The second scenario is where multithreading is used effectively. In this case, the threads wait for some external inputs and the processor can switch to another thread while the previous thread awaits input. This is rather a smarter way of executing programs without wasting computer’s processing power.
Multithreading in .NET
.NET is designed from scratch to support multithreading. There are two main ways of implementing multithreading in .NET; starting threads with ‘ThreadStart delegates’ (also known as the manual method), or using ‘ThreadPool class’.
If a thread is supposed to be running for a longtime, it is recommended to use ‘ThreadStart’ and manually initiate the thread. If the thread is to run for a brief period, then the smartest way is to use the ‘ThreadPool class’.
The Simplest Multithreading Example
Here is one of the most basic multithreaded programs written in C#.
Code: CSharp
using System;
using System.Threading;
public class TestThreading
{
static void Main()
{
ThreadStart job = new ThreadStart(ThreadJob);
Thread thread = new Thread(job);
thread.Start();
for (int i=0; i < 4; i++)
{
Console.WriteLine ("First Thread: {0}", i);
Thread.Sleep(1000);
}
}
static void ThreadJob()
{
for (int i=0; i < 8; i++)
{
Console.WriteLine ("Remaining Threads: {0}", i);
Thread.Sleep(500);
}
}
}
First Thread: 0
Remaining Threads: 0
Remaining Threads: 1
First Thread: 1
Remaining Threads: 2
Remaining Threads: 3
First Thread: 2
Remaining Threads: 4
Remaining Threads: 5
First Thread: 3
Remaining Threads: 6
Remaining Threads: 7
sbglobal
like this

