Wednesday, October 9, 2019

Thread

What is threading.


To define Threading in a one line, means parallel work or code execution. To perform any multiple task simultaneously means Threading.

For example executing Microsoft PPTX and Excel simultaneously in a desktop, laptop or any device is known as Threading.

Work or a code task is always been executed in a two ways i.e. Synchronous or Asynchronous way.


Synchronous way means where work multiple jobs are executed one after the other. Here Work 2 have to wait till Work 1 is completed same way the others.

Asynchronous:

Asynchronous means multiple work has been executed simultaneously like doing multitask at a same time.



For creating threading application there some important methods which used regularly for implementing threading.

1 : Thread Join

2 : Thread Sleep

3 : Thread Abort


Thread Join:


Thread.Join() make thread to finish its work or makes other thread to halt until it finishes work. Join method when attached to any thread, it makes that thread to execute first and halts other threads. Now on the same we will see a simple example where we apply Join method to thread.


Use of Thread Sleep:


Thread.Sleep a method used to suspend current thread for a specific interval of time. Time can be specified in milliseconds or Timespan. While in a Sleep mode a method does not consumes any CPU resources so indirectly it save memory for other thread processes.

On the same we will create a simple example where in the for loop while printing output we will make a thread to sleep for 4000 milliseconds i.e. 4 secs for per print and total we are going to print 6 outputs. To count it properly we have used .NET Diagnostics namespace which allows us to use Stopwatch and TimeSpan to count elapsedTime.



Use of Thread Abort

As name implies "Abort" so same way Thread.Abort helps to end or abort any thread to process it further. It raises ThreadAbortException in the thread for process of termination.


Types of Threads in C#

here are two types of Thread in Csharp i.e.
 1. Foreground Thread 
 2. Background Thread


Foreground Thread

As we know that Main method is also runs on single thread, So in a Main method when we attach any other method to a thread it means we are making a multithread application. Foreground threads are those threads which keeps running until it finishes his work even if the Main method thread quits its process. To make you understand more better let me show you. Lifespan of foreground threads does not depends on main thread.



Background Thread


Background thread is just opposite of foreground thread here background thread quits its job when main thread quits. Here lifespan of background threads depends on main thread. In order to implement background thread in a program we need to set property called IsBackground to true.





A property of type System.Threading.ThreadState, used to get the value containing the state of the thread.

Start()

Starts the execution of the thread.

Abort() 

Allows the current thread to stop the execution of the thread permanently.

Suspend() 

Pauses the execution of the thread temporarily.

Resume() 

Resumes the execution of a suspended thread.

Join() 


Make the current thread wait for another thread to finish.








No comments:

Post a Comment