Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Thread Concept in C#:-



   


Thread is a collection of light-weight subprocess to execute the program. means every program divided into multiple sub-unit that will be managed by Thread.

When we execute the c# program then Main() will work as a Main Thread which will be used to start the execution of the Program.



The thread has four different states:-



1)  Init State:-  When we create Thread Class Object

2)  Runnable State:-  When we start Thread

3) Waiting State:-  When we use sleep() in Thread

4) End State or Destroy State:-  When the process completed then end state


Type of Thread:-

1) Single Threading:- 


If we execute only one program at a time then it is called single thread
class ThreadExample
    {
        static void Display()
        {
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("Process " + i);
                Thread.Sleep(1000);
            }
        }
        static void Main()
        {
            ThreadStart th = new ThreadStart(ThreadExample.Display);
            Thread t = new Thread(th);
            t.Start();
            Console.ReadKey();
        }
    }

2) Multi-Threading:- 


 If we execute More then one Program simultaneous then it is called a multithreading concept.
The operating system is the best example of multithreading because it will execute multiple processes simultaneously.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication2
{
    class ThreadExample
    {
        static void Display()
        {
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("Process " + i);
                Thread.Sleep(1000);
            }
        }
        static void Main()
        {
            ThreadStart th = new ThreadStart(ThreadExample. Display);
            Thread t = new Thread(th);
            t.Start();
            Thread t1 = new Thread(th);
            t1.Start();
            Console.ReadKey();
        }
    }
}


Thread Synchronization :-

using this we can synchronize Thread Process Means When First Thread Will be executed after that another Thread will be completed. 

we will use Join() to synchronize Thread process because join will internally call wait() to another unitll first thread will execute.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication2
{
    class ThreadExample
    {
        static void Display()
        {
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("Process " + i);
                Thread.Sleep(1000);
            }
        }
        static void Main()
        {
            ThreadStart th = new ThreadStart(ThreadExample.Display);
            Thread t = new Thread(th);
            t.Start();
            t.Join();
            Thread t1 = new Thread(th);
            t1.Start();
            Console.ReadKey();
        }
    }
}



                       

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post