C# is a multi-paradigm programming language that supports several programming styles, including procedural, object-oriented, and functional programming. One of the essential features of C# is its support for multithreading, which enables developers to write applications that can perform multiple tasks concurrently.
Multithreading in C# is a programming technique that allows multiple threads to run concurrently within a single process. Each thread represents a separate execution path, enabling tasks to be performed in parallel, improving the efficiency and performance of applications.
Example: Implementation of multithreading using Thread class.
C#
// Creating a thread using the Thread class in C#
using System;
using System.Threading;
class Geeks
{
static void Main()
{
// create a new thread
Thread t = new Thread(Worker);
// start the thread
t.Start();
// do some other work in the main thread
for (int i = 1; i < 5; i++)
{
Console.WriteLine("Main thread doing some work");
Thread.Sleep(100);
}
// wait for the worker thread to complete
t.Join();
Console.WriteLine("Done");
}
static void Worker()
{
for (int i = 1; i < 3; i++)
{
Console.WriteLine("Worker thread doing some work");
Thread.Sleep(100);
}
}
}
OutputWorker thread doing some work
Main thread doing some work
Worker thread doing some work
Main thread doing some work
Main thread doing some work
Main thread doing some work
Done
Explanation: In the above example, we create and start a new thread that runs the Worker method while the main thread performs its task. Both threads print messages and sleep for a short duration. The main thread waits for the worker thread to complete before printing "Done".
Real-World Example
Multitasking is the simultaneous execution of multiple tasks or processes over a certain time interval. Windows operating system is an example of multitasking because it is capable of running more than one process at a time like running Google Chrome, Notepad, VLC player, etc. at the same time. The operating system uses a term known as a process to execute all these applications at the same time.
- Process: A Process is a part of an operating system that is responsible for executing an application.
- Thread: A thread is a lightweight process, or in other words, a thread is a unit which executes the code under the program.
Every program has logic and a thread is responsible for executing this logic. Every program by default carries one thread to execute the logic of the program and the thread is known as the Main Thread, so every program or application is by default single-threaded model.
Single Threaded Model
The single thread runs all the processes present in the program in a synchronizing manner, which means one after another. So, the second process waits until the first process completes its execution, it consumes more time in processing. For example, we have a class named Geek and this class contains two different methods, i.e. method1, and method2. Now the main thread is responsible for executing all these methods, so the main thread executes all these methods one by one.
Example: Illustration of Single-Threaded Model
C#
// Single threaded model example
using System;
using System.Threading;
public class Geek
{
public static void method1()
{
// It prints numbers from 0 to 4
for (int i = 1; i < 5; i++)
{
Console.WriteLine("Method1 is : {0}", i);
if (i == 2)
{
Thread.Sleep(100);
}
}
}
public static void method2()
{
// It prints numbers from 0 to 4
for (int j = 1; j < 5; j++)
{
Console.WriteLine("Method2 is : {0}", j);
}
}
}
public class Geeks
{
static public void Main()
{
// Calling static methods
Geek.method1();
Geek.method2();
}
}
OutputMethod1 is : 1
Method1 is : 2
Method1 is : 3
Method1 is : 4
Method2 is : 1
Method2 is : 2
Method2 is : 3
Method2 is : 4
Explanation: In the above example method 2 is waiting for method 1 until it is completed it will not work this is the drawback of a single-threaded model here to overcome this multithreading comes where we can run different programs and different threads simultaneously. Also maximizing the utilization of the CPU because multithreading works on a time-sharing concept means each thread takes its own time for execution and does not affect the execution of another thread, this time interval is given by the operating system.\
Multithreading allows the program to run multiple threads concurrently. It can significantly improve performance and responsiveness in applications.
Example: Illustration of Multithreading in C#
C#
// C# program to illustrate the
// concept of multithreading
using System;
using System.Threading;
public class Geeks
{
public static void method1()
{
for (int i = 1; i < 5; i++)
{
Console.WriteLine("Method1 is : {0}", i);
// sleep for 100 milliseconds
if (i == 2)
{
Thread.Sleep(100);
}
}
}
public static void method2()
{
// It prints numbers from 0 to 10
for (int j = 1; j < 5; j++)
{
Console.WriteLine("Method2 is : {0}", j);
}
}
static public void Main()
{
// Creating and initializing threads
Thread thr1 = new Thread(method1);
Thread thr2 = new Thread(method2);
thr1.Start();
thr2.Start();
}
}
OutputMethod1 is : 1
Method1 is : 2
Method2 is : 1
Method2 is : 2
Method2 is : 3
Method2 is : 4
Method1 is : 3
Method1 is : 4
Explanation: In the above example, we create and initialize two threads, i.e. thr1 and thr2 using the Thread class. Now using thr1.Start(); and thr2.Start(); we start the execution of both threads. Now both thread runs simultaneously and the processing of thr2 does not depend upon the processing of thr1 like in the single-threaded model.
Note: Output may vary due to context-switching
Ways to Implement Multithreading
In C#, multithreading is supported through the System. Threading namespace. There are several ways to create and manage threads described below:
- Thread Class
- Task Class
- Async and Await
- ThreadPool Class
1. Thread Class
The Thread class is the most basic way to implement multithreading in C#. We can create a thread by instantiating a Thread object and passing a method that represents the task to be executed.
Example: Creating threads using the Thread class.
C#
// Implementing Multithreading using Thread class
using System;
using System.Threading;
class Geeks
{
static void Main()
{
Thread thread1 = new Thread(PrintNumbers);
// Start the thread
thread1.Start();
PrintNumbers();
}
static void PrintNumbers()
{
for (int i = 1; i < 5; i++)
{
Console.WriteLine(i);
Thread.Sleep(100);
}
}
}
Explanation: In the above example, the PrintNumbers method runs in both the main thread and a separate thread1. The Start() method initiates the execution of the thread. Simulates some work by making the thread pause for a few seconds between iterations.
2. Task Class
Task class used for more complex or parallel tasks also known as Task Parallel Library (TPL). The Task class allows us to create tasks that can run asynchronously, improving both performance and code readability. It also simplifies task management by handling thread pooling and synchronization.
Example:
C#
// Implmentation of Multithreading in C# using Task Class
using System;
using System.Threading.Tasks;
class Geeks
{
static void Main()
{
// Running two tasks concurrently
Task task1 = Task.Run(() => PrintNumbers());
Task task2 = Task.Run(() => PrintNumbers());
// Wait for both tasks to complete
Task.WhenAll(task1, task2).Wait();
}
static void PrintNumbers()
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine(i);
}
}
}
Explanation: In the above example, two tasks are created using Task.Run(). These tasks execute the PrintNumbers method concurrently, improving overall performance. The Task.WhenAll() method ensures that both tasks are complete before the program finishes execution.
3. Async and Await
There is another way to perform multitasking we can use asynchronous tasks using the keyword async and await which are used for asynchronous programming, they can combine CPU-bound and I/O-bound operations. It is not a traditional multithreading, can be combined with a Task.Run for non-blocking multithreaded operations.
Example: Demonstratrating use of Async and Await in multithreaded environment.
C#
// Combining tasks with async/await to perform
// asynchronous operations
using System;
using System.Threading;
using System.Threading.Tasks;
class Geeks
{
static void Main()
{
// Example using Threads
Thread thread1 = new Thread(() => task("Thread 1"));
Thread thread2 = new Thread(() => task("Thread 2"));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Console.WriteLine("moving to task");
// Example using Tasks with async/await
Task.Run(async () => await RunAsyncTasks()).Wait();
Console.WriteLine("All tasks completed.");
}
static void task(string threadName)
{
for (int i = 1; i <= 2; i++)
{
Console.WriteLine($"{threadName} print: {i}");
Thread.Sleep(100); // Simulate work
}
}
static async Task RunAsyncTasks()
{
Task task1 = Task.Run(() => task("Task 1"));
Task task2 = Task.Run(() => task("Task 2"));
await Task.WhenAll(task1, task2);
}
}
OutputThread 1 print: 1
Thread 2 print: 1
Thread 1 print: 2
Thread 2 print: 2
moving to task
Task 1 print: 1
Task 2 print: 1
Task 1 print: 2
Task 2 print: 2
All tasks completed.
4. ThreadPool Class
ThreadPool class is a useful feature of C#. Which manages a pool of threads and can be used to execute tasks asynchronously and manages a pool of threads and can be used to execute tasks asynchronously. So Instead of creating and destroying threads for every task, which can be resource-intensive, the ThreadPool allows multiple tasks to share a limited number of threads. This leads to reduced overhead and improved performance, especially in applications that require frequent short-lived operations.
Example: Demonstration of ThreadPool Class in C#
C#
// Use of ThreadPool Class
using System;
using System.Threading;
class Geeks
{
static void Main()
{
// queue a work item to the thread pool
ThreadPool.QueueUserWorkItem(Worker, "Hello, world!");
// do some other work in the main thread
for (int i = 1; i < 5; i++)
{
Console.WriteLine("Main thread doing some work");
Thread.Sleep(100);
}
Console.WriteLine("Done");
}
static void Worker(object state)
{
string message = (string)state;
for (int i = 1; i < 5; i++)
{
Console.WriteLine(message);
Thread.Sleep(100);
}
}
}
OutputMain thread doing some work
Hello, world!
Hello, world!
Main thread doing some work
Main thread doing some work
Hello, world!
Hello, world!
Main thread doing some work
Done
Explanation: In this example, the Worker method is executed in a separate thread while the main thread is doing some other work. The Thread.sleep method is used to simulate some work being done in both threads.
Important Points:
- Deadlocks: Ensure that threads do not get stuck waiting for resources held by each other. Use lock carefully and try to lock resources in a consistent order.
- Thread Pooling: Use Task.Run() and the ThreadPool to avoid manually managing threads. Thread pooling reduces overhead and ensures efficient resource utilization.
- Time-Limit: Always provide a mechanism for cancelling long-running tasks to improve responsiveness, especially in UI applications.
- Shared Data: Minimize the use of shared data between threads to avoid race conditions. If shared data is necessary, ensure proper synchronization.
Advantages
- It executes multiple processes simultaneously.
- Maximize the utilization of CPU resources.
- Time sharing between multiple processes.
- Help to achieve Multitasking.
Similar Reads
Introduction
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers