![]() |
VOOZH | about |
Multithreading enables concurrent task execution in C#. It improves performance and responsiveness. We can create threads using the System.Threading namespace. With the help of Threads, we can achieve multitasking and can define their behavior using different methods provided by the Thread Class.
Example: This example shows how to create and start a thread using Thread Class in C#.
Main thread: 0 Worker thread: 0 Main thread: 1 Worker thread: 1 Main thread: 2 Worker thread: 2
Explanation: In this example, we create a new thread using the Thread class and pass in a delegate to the Worker method. We then start the thread using the Start method. Join() is used to wait until the worker thread completes.
Note: While creating thread make sure to properly synchronize access to shared data between threads to avoid race conditions and other synchronization issues.
First, import the System.Threading namespace. It plays an important role in creating a thread in our program, as it allows us to use thread-related classes without specifying their fully qualified names every time.
using System;
using System.Threading;
Now, create and initialize the thread object in main method.
public static void Main()
{
Thread thr = new Thread(job1);
}
Start the thread using the Start method.
public static void Main()
{
Thread thr = new Thread(job1);thr.Start();
}
Example:
First Thread First Thread First Thread
Explanation: In the above example, we create an instance (obj) of the ExThread class and pass its method (mythread1) to the ThreadStart delegate. Then, we create a thread (thr) and start it using thr.Start() to execute the method in a separate thread.
Example:
0 1 2 0 1 2
Explanation: In the above example, The ExThread contains two static methods thread1() and thread2(). So we do not need to create an instance of ExThread class. Here, we call these methods using a class name, we create and initialize the work of thread a, similarly for thread b. By using a.Start(); and b.Start(); statements, a and b threads scheduled for execution.
Note: The output of these programs may vary due to context switching.
In C# Thread Class is used to create and manage threads, providing greater control over multithreading. It allows operations such as setting thread priorities, managing thread states, and handling synchronization. We can perform multiple operations on threads, set thread priorities, manage thread states, and handle synchronization mechanisms to prevent race conditions.
Example:
Thread is running....
In C# Task class is part of the Task Parallel Library (TPL) and is used for managing and running asynchronous operations or parallel tasks. It simplifies multithreading by abstracting the complexity of thread management and provides a more scalable and efficient way to handle concurrent operations.
Example:
Task is running......
The ThreadStart delegate in C# specifies the method to execute when a thread starts. It represents a method that takes no parameters and returns void.
Example:
Program start Thread started.. Thread finished..
In C#, a multi-threading system is built upon the Thread class, which encapsulates the execution of threads.
Class | Description |
|---|---|
Thread | This is used to create and control a thread, set its priority, and get its status. |
Mutex | It is a synchronization primitive that can also be used for IPC (Inter-Process Communication). |
Monitor | This class provides a mechanism that accesses objects in a synchronized manner. |
Semaphore | Used to limit the number of threads that can access a resource or pool of resources concurrently. |
ThreadPool | Provides a pool of threads that can be used to execute tasks, post work items, process asynchronous I/O, wait on behalf of other threads, and process timers. |
ThreadLocal | Provides thread-local storage of data. |
Timer | Provides a mechanism for executing a method on a thread pool thread at specified intervals. We are not allowed to inherit this class. |
Volatile | Contains methods for performing volatile memory operations |