Thread Concept in C#

0
Thread Concept in C#:-

Thread is a collection of lightweight subprocesses to execute the program. means every program is 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.

If we want to execute multiple programs under a single resource at a time then we use the multithreading concept that will be implemented into the real-time applications.


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 is completed then the end state


Type of Thread:-

1) Single Threading:- 


If we execute only one program at a time then it is called a 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 than one program simultaneously 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 proceed 

we will use Join() to synchronize the 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();
        }
    }
}



                       
Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)