Introduction
Multithreading is considered complicated (though it's not) and today it is the need of nearly every developer to understand it, at least to some extent. This article explains the basics of multithreading and how it can be done. Multithreading is nothing but doing more than one thing at a time. Multithreading makes use of the CPU core that helps in optimizing the performance of applications.
Overview
Before getting into multithreading, let's understand what a thread is. A thread is a lightweight process, with an independent execution path. Whenever we have a complex or time consuming scenario, it is better to create multiple threads with various execution paths that can run independently. All these threads are called worker threads. These worker threads fulfill their tasks without waiting for the main thread to complete execution. The issue with multithreading is resource-sharing that may lead to deadlock situations.
How it works
The .NET namespace for multithreading is System.Threading. There are two ways by which multithreading can be done in an application:
- Creating multiple threads and using ThreadStart delegate.
- Using asynchronous methods like BeginInvoke.
Creation of a Simple Thread
To begin with, let's start by creating a normal thread. For creating a simple thread, an instance of the Thread class is required. The constructor of this class takes reference of a ThreadStart class that points to the method that is executed when the thread is started. The thread starts its execution when the Start() method of the thread is invoked. Refer to the following code snippet for that:
- namespace Sample
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Inside main");
-
-
- Thread th = new Thread(new ThreadStart(new Program().PrintThread));
-
-
- th.Start();
- Console.ReadLine();
- }
-
- public void PrintThread()
- {
- Console.WriteLine("In print function.");
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("Print "+ i + " times.");
- Thread.Sleep(1000);
- }
- }
- }
- }
Other frequently used Thread class methods are the following:
- Sleep: this is used for pausing the thread for a specified period of time.
- Abort: this is used for destroying the threads.
- Suspend: is used for pausing the thread when it reaches a safe point.
- Resume: to restart the suspended thread.
- Join: it causes the current thread to wait for another thread to finish.
Now let's consider a simple example of multithreading.
The following code is an example of how a thread execution occurs. Here the first step is to define a ThreadStart instance with the function that we want to execute in the thread. The Start method of the System.Threading namespace will then be called when we want to invoke the method that will run in parallel with other processes.
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Inside main");
-
-
- Thread th = new Thread(new ThreadStart(new Program().PrintThread));
-
-
- th.Start();
-
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("Print main thread" + i + " times.");
- Thread.Sleep(1000);
- }
- Console.ReadLine();
- }
-
- public void PrintThread()
- {
- Console.WriteLine("In print function.");
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("Print other thread"+ i + " times.");
- Thread.Sleep(500);
- }
- }
- }
I hope some of the basics of multithreading has been clarified in this article. Keep following for more articles on multithreading. Until then, Happy Programming.