Introduction to Multithreading: Part 1

In this article you will learn the basics of Multithreading. It makes use of CPU core which helps in optimizing the performance of any application.

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:

  1. Creating multiple threads and using ThreadStart delegate.
  2. 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:

  1. namespace Sample  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Console.WriteLine("Inside main");  
  8.   
  9.             //Creation of instance of Thread class  
  10.             Thread th = new Thread(new ThreadStart(new Program().PrintThread));  
  11.   
  12.             //Invoking Start method, here the thread will start executing  
  13.             th.Start();  
  14.             Console.ReadLine();  
  15.         }  
  16.   
  17.         public void PrintThread()  
  18.         {  
  19.             Console.WriteLine("In print function.");  
  20.             for (int i = 0; i < 10; i++)  
  21.             {  
  22.                 Console.WriteLine("Print "+ i + " times.");  
  23.                 Thread.Sleep(1000);  
  24.             }  
  25.         }  
  26.     }  
  27. }  
Other frequently used Thread class methods are the following:
  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 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.

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         Console.WriteLine("Inside main");  
  6.   
  7.         //Creation of instance of Thread class  
  8.         Thread th = new Thread(new ThreadStart(new Program().PrintThread));  
  9.   
  10.         //Invoking Start method, here the thread will start executing  
  11.         th.Start();  
  12.   
  13.         for (int i = 0; i < 10; i++)  
  14.         {  
  15.             Console.WriteLine("Print main thread" + i + " times.");  
  16.             Thread.Sleep(1000);  
  17.         }  
  18.         Console.ReadLine();  
  19.     }  
  20.   
  21.     public void PrintThread()  
  22.     {  
  23.         Console.WriteLine("In print function.");  
  24.         for (int i = 0; i < 10; i++)  
  25.         {  
  26.             Console.WriteLine("Print other thread"+ i + " times.");  
  27.             Thread.Sleep(500);  
  28.         }  
  29.     }  
  30. }  
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.