C# Multithreading


Threading is a lightweight process, Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time so Multithreading in C# is a process in which multiple threads work simultaneously .Form multithreading we need to use System.Threding namespace. A list of commonly used classes of System.Threading namespace are given below:
·         Thread
·         Mutex
·         Timer
·         Monitor
·         Semaphore

Example of multithreading:

using System;
using System.Threading;

public class MultithreadingExampleClass
{
    static void Main()
    {
        ThreadStart job = new ThreadStart(ThreadJob);
        Thread thread = new Thread(job);
        thread.Start();

        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Main thread: {0}", i);
            Thread.Sleep(1000);
        }
    }

    static void ThreadJob()
    {
        for (int i = 0; i < 20; i++)
        {
            Console.WriteLine("Other thread: {0}", i);
            Thread.Sleep(500);
        }
    }
}