Thread Concept in C#:-
Main method. However, you can create additional threads to run tasks in parallel.🔹 What is a Thread?
A thread represents a path of execution inside a program.
-
A process can contain multiple threads
-
All threads share the same memory
-
Each thread has its own stack and execution flow
Example:
-
Main thread → handles UI
-
Background thread → downloads data
-
Another thread → processes data
Multithreading Concept in C#:-
| Multithreading | Multiple threads of one program |
| Multiprocessing | Multiple programs/processes |
| Multitasking (OS) | Multiple applications |
🔍 Real-Time Application Examples
-
Web servers handling multiple requests
-
Banking transaction systems
-
Video streaming apps
-
Chat applications
-
Games (rendering + input + sound)
2) Runnable State:- When we start Thread
3) Waiting State:- When we use sleep() in Thread
4) End State or Destroy State:- When the process is completed then the end state
Type of Thread:-
1) Single Threading:-
class ThreadExample
{
static void Display()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("Process " + i);
Thread.Sleep(1000);
}
}
static void Main()
{
ThreadStart th = new ThreadStart(ThreadExample.Display);
Thread t = new Thread(th);
t.Start();
Console.ReadKey();
}
}
2) Multi-Threading:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication2
{
class ThreadExample
{
static void Display()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine("Process " + i);
Thread.Sleep(1000);
}
}
static void Main()
{
ThreadStart th = new ThreadStart(ThreadExample. Display);
Thread t = new Thread(th);
t.Start();
Thread t1 = new Thread(th);
t1.Start();
Console.ReadKey();
}
}
}
What Join() Actually Does
Join()makes the calling thread wait until the specified thread completes its execution.
-
It is used for execution order control
-
It is not a general synchronization mechanism
-
It does not synchronize shared data
Thread Synchronization
Thread Synchronization Using lock
🔹 Concept
-
Two threads try to access the same method
-
lockensures only one thread executes it at a time
What lock Does (Simple Words)
-
Allows only one thread at a time
-
Protects critical section
-
Prevents race condition

0 Comments
POST Answer of Questions and ASK to Doubt