Introduction:

Multithreading is considered as complicated (though it’s not), and today it is the need of almost every developer to understand it at least to some extent. This article will explain you the basics of multithreading and how they can be performed. Multithreading is nothing but doing more than one thing at a time. Multithreading makes use of CPU core which helps in optimizing the performance of any application.

Overview:

Before moving ahead to 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 scenarios, it is better to create multiple threads with different execution path which can run independently. All these threads are called as worker threads. These worker threads fulfill their tasks without waiting for the main thread to complete execution. The issue with multithreading is resource-sharing which may leads to deadlock situations.

How it works?

The .Net namespace for multithreading is System.Threading. There are two ways by which multithreading can be achieved in an application:

1. Creating different threads and using ThreadStart delegate
2. Using asynchronous methods like BeginInvoke

Creation of a Simple Thread:

To begin with, let’s start by creation of a normal thread. For creating a simple thread, an instance of Thread class is required. The constructor of this class takes reference of a ThreadStart class, which points to the method that is executed when the thread is started. The thread starts its execution when the Start() method of thread is invoked. Refer the below code snippet for the same:

namespace Sample

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Inside main");

//Creation of instance of Thread class

Thread th = new Thread(new ThreadStart(new Program().PrintThread));

//Invoking Start method, here the thread will start executing

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 majorly used Thread class methods are:

1. Sleep: this is used for pausing the thread for a specified period of time.
2. Abort: this is used for destroying the threads.
3. Suspend: is used for pausing the thread when it reaches a safe point.
4. Resume: to restart the suspended thread.
5. 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 will tell how a thread execution occurs. Here the first step is to define a ThreadStart instance with the function which we want to execute in the thread. The Start method of System.Threading namespace will then be called when we want to invoke the method which will run in parallel to other processes.

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Inside main");

//Creation of instance of Thread class

Thread th = new Thread(new ThreadStart(new Program().PrintThread));

//Invoking Start method, here the thread will start executing

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 basics of multithreading will be cleared in this article. Keep following the post for more articles on multithreading. Till then, Happy Programming J