Multi-Threading in C# (Interview Guide)
Multi-threading allows a program to run multiple tasks at the same time using multiple threads.
A Thread is the smallest unit of execution inside a process.
👉 Example:
-
Download file 📥
-
Show progress bar 📊
-
Allow user interaction 🖱️
All happening simultaneously using multi-threading.
Why Multi-Threading? (Interview Answer)
Multi-threading is used for:
-
Better performance ⚡
-
Responsive UI 🖥️
-
Parallel execution 🔄
-
Background processing 🔧
Process vs Thread (Important Interview Question)
| Process | Thread |
|---|---|
| Independent program | Part of a process |
| Heavyweight | Lightweight |
| Separate memory | Shared memory |
| Slower | Faster |
Example:
-
Browser = Process
-
Tabs = Threads
Example Without Multi-Threading
static void Main()
{
Method1();
Method2();
}
static void Method1()
{
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Method1");
}
}
static void Method2()
{
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Method2");
}
}
Output (Sequential)
Method1
Method1
Method1
Method1
Method1
Method2
Method2
Method2
Method2
Method2
This is Single Threaded.
Multi-Threading Example Using Thread Class
using System.Threading;
static void Main()
{
Thread t1 = new Thread(Method1);
Thread t2 = new Thread(Method2);
t1.Start();
t2.Start();
}
static void Method1()
{
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Method1");
}
}
static void Method2()
{
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Method2");
}
}
Output (Parallel)
Method1
Method2
Method1
Method2
Method1
Method2
Execution order is not guaranteed.
Task Parallel Library (Recommended in Modern C#)
using System.Threading.Tasks;
static void Main()
{
Task task1 = Task.Run(() => Method1());
Task task2 = Task.Run(() => Method2());
Task.WaitAll(task1, task2);
}
👉 Task is preferred over Thread in modern applications.
Thread.Sleep Example
static void Method1()
{
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Method1");
Thread.Sleep(1000);
}
}
This pauses execution for 1 second ⏱️
Real-World Example 🏢
A Jobportal may do:
-
Send email 📧
-
Upload resume 📄
-
Save database 💾
All at same time using multi-threading.
Task.Run(() => SendEmail());
Task.Run(() => UploadResume());
Task.Run(() => SaveToDatabase());
This improves performance and user experience.
Thread vs Task (Interview Favorite Question)
| Thread | Task |
|---|---|
| Low-level | High-level |
| Hard to manage | Easy to manage |
| Manual control | Automatic |
| Slower | Faster |
👉 Always prefer Task in modern C#
Multi-Threading Problems
1. Race Condition
int counter = 0;
Parallel.For(0, 1000, i =>
{
counter++;
});
Expected: 1000
Actual: unpredictable ❌
Solution: Lock
lock(obj)
{
counter++;
}
Lock Example 🔐
static object obj = new object();
lock(obj)
{
Console.WriteLine("Thread Safe");
}
Parallel Programming Example
Parallel.For(0, 5, i =>
{
Console.WriteLine(i);
});
Runs in parallel ⚡
Interview Questions & Answers
1. What is Multi-Threading?
Running multiple threads simultaneously within a process.
2. What is Thread?
Smallest unit of execution.
3. Difference between Task and Thread?
Task is high-level and recommended.
4. What is Race Condition?
Multiple threads accessing shared resource simultaneously.
5. What is Deadlock?
Two threads waiting for each other forever.
Deadlock Example
lock(obj1)
{
lock(obj2)
{
}
}
Another thread:
lock(obj2)
{
lock(obj1)
{
}
}
This causes Deadlock.
When to Use Multi-Threading
Use when:
✅ File processing
✅ Background jobs
✅ Email sending
✅ API calls
✅ Parallel computation
Avoid when:
❌ Simple operations
❌ Shared resource heavy operations
Best Practices
✔ Use Task instead of Thread
✔ Avoid shared variables
✔ Use async/await when possible
✔ Use lock carefully
✔ Avoid deadlocks
Async vs Multi-Threading (Important Interview Question)
| Async | Multi-Threading |
|---|---|
| Non-blocking | Parallel execution |
| Single thread possible | Multiple threads |
| I/O operations | CPU operations |
Example:
await GetDataAsync();
No extra thread required.
Summary
-
Multi-Threading improves performance
-
Thread is low-level
-
Task is recommended
-
Watch for race conditions
-
Use lock carefully
