Skip to main content

Thread

*■ 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

Popular posts from this blog

Raster scan Vs Vector Scan

1. Raster Scan Display   How It Works : A raster scan display works by painting an image on the screen pixel by pixel, row by row. It follows a systematic pattern where the electron beam (in CRT monitors) or the display elements (in modern LCD/LED screens) sweep across the screen from left to right, top to bottom, in a series of horizontal lines (scan lines). This process is akin to how a traditional TV screen works.   Process : The display draws the image starting from the top-left corner, moving to the right, then moves to the next row below, and repeats this process until the entire screen is filled. This pattern creates a grid of pixels, where each pixel can have a color and brightness level.   Characteristics : Pixel-based : The screen consists of a grid of pixels, and each pixel can have a distinct color and intensity. Continuous Image : Raster scan displays are capable of displaying detailed and complex images, including photographs and videos, because they break t...

Inheritance

*■ Inheritance*  • Inheritance is a concept in OOP that allows a class to inherit properties and behaviors (methods) from another class. • A class that inherits from another class is called a derived class (or subclass) • The class which gets inherited by another class is called the base class (or superclass). • Inheritance is possible only if there is is-a relationship between parent and child class. • constructors are not inherited in derived class, however the derived class can call default constructor implicitly and if there's a parameterised constructors in bass class then derived class can call it using 'base' keyword.  ____________________________________________  *➤ Rules of Inheritance*  1) C# supports single inheritance, meaning a class can inherit from only one base class. 2) A parent class constructor must be accessible in child class otherwise  inheritance will be not possible. 3) every class, whether user-defined or predefined implicitly derives fr...

unit -1 Introduction of Image processing

  What is Image Processing? Image processing is a method to perform operations on an image to enhance it or extract useful information. It is a type of signal processing where the input is an image, and the output may be either an image or characteristics/features associated with that image. Goals of Image Processing Image Enhancement : Improving visual appearance (e.g., contrast, sharpness) Image Restoration : Removing noise or distortion Image Compression : Reducing the amount of data required to represent an image Feature Extraction : Identifying objects, edges, or patterns Image Analysis : Understanding and interpreting image content Object Recognition : Detecting and identifying objects in an image What is an Image? An image is a two-dimensional function f(x, y) , where x and y are spatial coordinates, and f is the intensity (brightness or color) at that point. For digital images, both x, y, and f are finite and discrete. Types of Image Representation...