*■ Thread*
• a thread is a unit of execution within a process which executes an code under an application. It allows multiple operations to run concurrently.
• thread is a light weight process.
• A thread is a single sequence of instructions that a process can execute
• When a C# program starts, it’s a single threaded process by default.
• This “main” thread is responsible for executing your code line by line, creating what is known as a single threaded application.
• Every application has some logic in it and to execute that logic this thread come into picture.
• By default every application contain one thread to execute the program, and that is known as Main Thread.
• by default, a thread in C# does not have a name unless you explicitly set it.
• Drawback - in a single-threaded program, the entire logic runs sequentially, one task at a time. This means the program executes each line of code in the order it appears. If a piece of code takes a long time to run (like reading a file or waiting for user input), the whole program will pause and wait until that task is finished before moving on to the next one.
So to overcome this problem multithreading concept come into picture.
*➤ Thread life cycle*
• each thread has a life cycle.
• The life cycle of a thread is started when the instance of System.Threading.Thread is created.
• and when the task execution of the thread is completed, it's life cycle is ended.
• here are the following states in the life cycle of thread.
1) Unstarted: The thread is created but hasn’t started yet. You’ve created a Thread object, but you haven’t called the Start() method.
2) Ready (Runnable): Once you call the Start() method, the thread moves to the ready state. It's ready to run but waits for the CPU to allocate time to it.
3) Running: The thread is actively executing its task. The CPU has assigned time for this thread to run.
4) Blocked/Waiting: The thread might move to a blocked state if it’s waiting for some resource (like I/O operations or a lock). The thread is paused and waiting for an event to occur before it can continue running.
Example - A thread might be blocked if it’s waiting for input or sleeping.
5) Terminated (Dead): Once the thread completes its execution, it enters the terminated state and cannot be restarted.
This happens when the method that the thread was executing has completed.
*➤ Thread classes*
Thread class provides properties and methods to create and control threads. It is found in System.Threading Namespace.
➔ Key members of the thread class
1) creating a thread
2) Starting a Thread: Use the Start() method to begin thread execution.
3) Properties
Name: Gets or sets the name of the thread.
IsAlive: Returns true if the thread is running or in a blocked state.
IsBackground: Indicates whether a thread is a background thread (can be set to true or false).
Priority: Gets or sets the priority of a thread (can be Lowest, BelowNormal, Normal, AboveNormal, Highest).
ThreadState: Indicates the current state of the thread.
*➤ Multithreading*
• Multithreading is a programming technique where multiple threads are created within a single process to perform different tasks simultaneously.
• Multithreading allows for the concurrent execution of threads, meaning multiple threads can be in progress at the same time, although not necessarily simultaneously.
• Threads within the same process share the same memory and resources, making communication between threads easier but also introducing potential issues like race conditions and deadlocks.
*➔ Common Multithreading Issues*
• Race Conditions: Occur when two or more threads access shared data at the same time, leading to inconsistent results.
• Deadlocks: Occur when two or more threads are waiting indefinitely for resources locked by each other.
• Thread Safety: Ensuring that shared data is accessed in a thread-safe manner to avoid conflicts
➔ Benefits of Multithreading
• Improved Performance: By running multiple threads, you can take full advantage of CPU resources, especially on multi-core processors.
• Responsiveness: In UI applications, multithreading allows the user interface to remain responsive while performing background tasks.
• Efficient Resource Use: Threads can handle I/O-bound tasks (like reading a file or making a network request) while other threads continue processing, making better use of system resources.
Comments
Post a Comment