Multithreading in C#
Multithreading in C#
Using Multithreading in
ASP.NET Core
MultiThreading in C#
In simple terms
Keivan Damirchi
Include:
What is MultiThreading?
Remember
A process represents an application whereas a thread
represents a module of the application
1
Example to Understand
Threading
Creates two threads, each with a separate method to execute concurrently.
After creating
and starting
both threads,
the main
thread waits
for them to
finish using the
Join() method,
which blocks
until the
threads
complete.
Finally, the program prints "Finished." to the console to indicate that both threads have
completed.
2
Real-World Scenarios in ASP.NET Core
Tips
When multiple threads are accessing shared resources, it is
important to use synchronization techniques such as locks,
semaphores, or atomic operations to prevent race conditions
and other concurrency-related issues.
3
Real-World Scenarios in ASP.NET Core
You can use multithreading to run long-running tasks in the background. For
example, you can use the BackgroundService class to create a background
service that runs in the background and performs a task, such as processing
files.
Tips
Blocking operations, such as I/O or waiting for a lock, can cause a
thread to become unresponsive and degrade the performance of the
program. As much as possible, use asynchronous operations or
non-blocking synchronization techniques.
4
Real-World Scenarios in ASP.NET Core
Tips
Blocking operations, such as I/O or waiting for a lock, can cause a
thread to become unresponsive and degrade the performance of the
program. As much as possible, use asynchronous operations or
non-blocking synchronization techniques.
5
Real-World Scenarios in ASP.NET Core
Parallel Processing
You can use multithreading to perform multiple tasks in parallel. For example,
you can use the Parallel.ForEach method to process a list of files in parallel.
Tips
Thread pools are a useful way to manage the creation and reuse of
threads in a program. However, creating too many threads or using
long-running tasks can exhaust the thread pool and degrade the
overall performance of the program.
6
Real-World Scenarios in ASP.NET Core
Data Processing
You can use multithreading to divide large amounts of data into smaller
chunks and process them in parallel. For example, you can use the
Parallel.ForEach method to process a large dataset in parallel.
Tips
Thread starvation can occur when a high-priority thread is blocked by
a low-priority thread. To avoid this, use appropriate priority levels and
avoid long-running operations in high-priority threads.
7
Real-World Scenarios in ASP.NET Core
Scheduling Tasks
Tips
Terminating a thread abruptly can lead to data corruption or other
unexpected behavior. Use safe termination techniques such as setting
a flag or using a cancellation token to signal the thread to exit
gracefully.
8
Real-World Scenarios in ASP.NET Core
Real-Time Processing
Tips
Multi-threaded programs can be difficult to test and debug. Use unit
testing and profiling tools to identify and fix concurrency-related
issues.
9
Life Cycle of a thread
In many texts, maximum 5 following situations are considered for an
Thread:
Unstarted State | Runnable State | Running State | Not Runnable State | Dead State
Unstarted: This is the initial state of a thread when it is created, but its Start()
Method has not yet been called.
Ready: A thread is in this state when it has been created and its Start() method has
been called, but it is waiting for a processor to become available to run.
Running: A thread enters this state when the operating system has assigned a
processor to execute its code.
Waiting: A thread enters this state when it is blocked, waiting for a particular
event to occur, such as input/output or synchronization.
Suspended: A thread enters this state when it is temporarily stopped by the operating
system or another thread, but it can be resumed later.
Aborted: A thread enters this state when its Abort() method is called, indicating
that it should be terminated.
Stopped: A thread enters this state when its code has finished executing or when
an unhandled exception occurs.
10
When to Thread and When not to Thread
When to Thread
11
When to Thread and When not to Thread
12
Common Concepts Related to
Threading
Thread
Mutex
Timer
Monitor
13
Common Concepts Related to
Threading
Semaphore
Thread
Local
Thread
Pool
Volatile
14
Frequently Used Thread
Properties
15
Frequently Used Thread
Methods
16
Close();
Keivan Damirchi