Threading is very useful, when we are doing a multitask in a program. It will improve the application performance, if we us it in the proper way. Generally, we are writing the programs where a single thread runs in a process at a time. However, in this way, the Application uses one thread to perform the multiple jobs in a processor.
To execute more than one task at a time, it can be divided into smaller threads and we can feel it as a concurrent programming in a processor. For example, a process can run one thread at a time and after some fraction of time the next thread will come and run, the first thread will go in to the waiting state, while the next thread is running and wait for its time.
In this way, we can feel that a multitask is happening concurrently in a process, but actually the thread is running one after another in a very short interval of time. Threads are known as lightweight processes.
The following states in the life cycle of a thread are:
To execute more than one task at a time, it can be divided into smaller threads and we can feel it as a concurrent programming in a processor. For example, a process can run one thread at a time and after some fraction of time the next thread will come and run, the first thread will go in to the waiting state, while the next thread is running and wait for its time.
In this way, we can feel that a multitask is happening concurrently in a process, but actually the thread is running one after another in a very short interval of time. Threads are known as lightweight processes.
The following states in the life cycle of a thread are:
- Unstarted State: When the instance of the thread is created but the Start method is not called.
- Ready State: When the thread is ready to run and waits for CPU cycle.
- Sleep method has been called.
- Wait method has been called.
- Blocked by I/O operations.
The Dead State: When the thread is in the condition, given below:
- Completes execution.
- Aborted.
Here, we are going to create one sample Application to show how multi-thread is working in an Application.
First of all open Visual Studio, Go to File -> New - > Project -> choose as a console Application and enter the name of a project - “ThreadSample”, shown below in the image.
Create another class PrintNumber inside the Program class, shown below:
First of all open Visual Studio, Go to File -> New - > Project -> choose as a console Application and enter the name of a project - “ThreadSample”, shown below in the image.
Create another class PrintNumber inside the Program class, shown below:
- public class PrintNumber
- {
- public void Print()
- {
- for (long i = 1; i <= 100; i++)
- {
- Console.WriteLine(i);
- }
- }
- }
- static void Main(string[] args)
- {
- PrintNumber printNumber = new PrintNumber();
- Thread t1 = new Thread(printNumber.Print);
- Thread t2 = new Thread(printNumber.Print);
- t1.Start();
- t2.Start();
- Console.ReadKey();
- }
Here, in the example, shown above, we can clearly see the output, where the first thread and second thread are running independently.
Multithreading Overview
A thread is an independent stream of instructions in a program. A thread is similar to a sequential program. However, a thread itself is not a program, it can't run on its own, instead it runs within a program's context.
The real usage of a thread is not about a single sequential thread, but rather using multiple threads in a single program. Multiple threads running at the same time and performing various tasks is referred as Multithreading. A thread is considered to be a lightweight process because it runs within the context of a program and takes advantage of resources allocated for that program.
With the task manager, you can turn on the Thread column and see the processes and the number of threads for every process. Here, you can notice that only cmd.exe is has a single thread whereas all other applications use multiple threads.
The operating system schedules threads. A thread has a priority and every thread has its own stack, but the memory for the program code and heap are shared among all threads of a single process.
A process consists of one or more threads of execution. A process always consists of at least one thread called the primary thread (Main() method in C# programs). A single-threaded process contains only one thread while a multithreaded process contains more than one thread for execution.
On a computer, the operating system loads and starts applications. Each application or service runs as a separate process on the machine. The following image illustrates that there are quite a few processes actually running than there are actual applications running in the system. Many of these processes are background operating system processes that are started automatically when the OS loads.
System.Threading Namespace
Like many other features, in .NET, System.Threading is the namespace that provides various types to help in construction of multithreaded applications.
No comments:
Post a Comment