Ex 5
Ex 5
Listen Share
1. Basics of Threading
What is a Thread?
A thread is the smallest unit of execution within a process. Each thread has its own
stack and local variables. In C#, the main thread is the one that executes the Main
method. However, you can create additional threads to run tasks in parallel.
Creating a Thread
To create a thread in C#, you can use the Thread class from the System.Threading
namespace.
using System;
using System.Threading;
class Program
{
static void Main()
{
Thread newThread = new Thread(new ThreadStart(PrintNumbers));
newThread.Start();
}
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 1/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
In the above example, we created a new thread that runs the PrintNumbers method.
2. Thread Lifecycle
A thread goes through various states in its lifecycle:
You can check the state of a thread using the ThreadState property.
3. Thread Synchronization
When multiple threads access shared resources, there's a potential for race
conditions. To prevent this, you can use synchronization techniques.
Lock
The lock keyword ensures that one thread does not enter a critical section while
another thread is in the critical section.
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 2/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
Mutex
A mutex is similar to a lock but can work across multiple processes.
Thread Pooling
Instead of creating and destroying threads, which can be resource-intensive, you
can use a thread pool. The ThreadPool class allows you to queue tasks without
managing individual threads.
ThreadPool.QueueUserWorkItem((state) => {
Console.WriteLine("This is running in a thread pool thread.");
});
Tasks
Tasks represent an asynchronous operation. They are built on top of the thread pool
and provide a higher-level way to work with concurrency.
using System.Threading.Tasks;
Task.Run(() => {
Console.WriteLine("This is running in a task.");
});
Parallel Loops
The Parallel class provides a way to parallelize loops, making them run faster on
multi-core systems.
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 3/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
Conclusion
Threading in C# offers a powerful way to improve application performance by
leveraging concurrency. Whether you're just starting out or looking to dive deep into
advanced concepts, the System.Threading namespace provides all the tools you
need. Remember to always be cautious when working with threads, as improper
synchronization can lead to unpredictable results. Happy coding!
Follow
Software Architect & .NET expert. Specializing in Docker & Kubernetes. Freelance corporate trainer. Shaping
tech & sharing insights on Medium.
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 4/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
Search
Laks Tutor
14
Laks Tutor
Introduction
53 1
Laks Tutor
57
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 6/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
Laks Tutor
14
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 7/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
Niraj Ranasinghe
94
Shahzad Aslam
Polymorphism mean many forms that we usually archived with static binding and dynamic
binding.. In static polymorphism, the response to a…
30
Lists
Staff Picks
636 stories · 957 saves
Self-Improvement 101
20 stories · 1766 saves
Productivity 101
20 stories · 1627 saves
Konstantin Fedorov
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 9/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
125
Sergio Gonzalez
130 3
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 10/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
43 1
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 11/12
5/10/24, 4:15 PM C# Threading: From Basic to Advanced | by Laks Tutor | Medium
18 1
https://medium.com/@lexitrainerph/c-threading-from-basic-to-advanced-84927e502a38 12/12