blog

Home / DeveloperSection / Blogs / Multithreading in csharp

Multithreading in csharp

Manish Kumar1769 26-Feb-2017

It is the path of Execution. Each thread defines the unique flow of control .Threads are lightweight process. Use of threads saves wastage of CPU cycle and increase efficiency of an application

It means it perform one task at a time

Multithreading     

When we perform multiple task at the same time this is knowns as multithreading.

Advantages-

Save time

Optimize the use of computer resources such as memory

Foreground thread- Foreground threads are those thread  in which main application is exited but it continue running whenever foreground thread is not stopped.

 Background thread-Background thread is die off if the application is finished

 Sequential execution of program example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
 
namespace ConsoleApplication9
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Funtion1();
            Function2();
           
           
        }
        publicstaticvoid Funtion1()
        {
          
           
            Console.WriteLine("function 1 start");
            for (int i = 1; i < 10; i++)
            {
              
                Console.WriteLine("we are in function1");
                Thread.Sleep(4000);
             
            }
            Console.WriteLine("function 1 end");
        }
        publicstaticvoid Function2()
        {
            Console.WriteLine("function 2 start");
            for (int i = 1; i < 10; i++)
            {
                Console.WriteLine("we are in function2");
                Thread.Sleep(4000);
 
            }
            Console.WriteLine("function 2 end");
        }
    }
}
 
When we run the application it gives the output as below

Multithreading in csharp

In the above example we have saw sequential execution of program mean it run sequentially at first function 1 run after completing then it start function 2

Parallel execution of program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread fun1 = new Thread(Funtion1);
            fun1.Start();
            Thread fun2 = new Thread(Function2);
            fun2.Start();

        }
        public static void Funtion1()
        {
            Console.WriteLine("function 1 start");
            for (int i = 1; i < 10; i++)
            {
                Console.WriteLine("we are in function1");
                Thread.Sleep(4000);
            }
            Console.WriteLine("function 1 end");
        }
        public static void Function2()
        {
            Console.WriteLine("function 2 start");
            for (int i = 1; i < 10; i++)
            {
                Console.WriteLine("we are in function2");
                Thread.Sleep(4000);
            }
            Console.WriteLine("function 2 end");
        }
    }
}

You can also visit these related useful blogs

Updated 17-Mar-2018

Leave Comment

Comments

Liked By