Synchronous and Asynchronous Threads in C sharp

Synchronous and Asynchronous Threads in C # – Learn how to executes tasks  parallel to minimize the execution time using multithreading.

Work or code task always executed in 2 ways
1. Synchronous
2. Asynchronous

Synchronous way means where multiple jobs executed one after another. dependency on another work completion is more in synchronous way.
work1–>work2–>…. This is also called synchronous threading.

For Example, In our Iphone , we can do only one task either we can listen the songs or we can keep charging to our phone. both we can’t do at a time.
This is example for synchronous threading.

Now to make you understand more better we will create two methods i.e. ListeningMusic and ChargingIphone respectively and inside that we will make a FOR loop.
Here we want to test how both functions are executed.

C# Threading Example with step by step execution

Code

class Iphone
    {
        static void Main(string[] args)
        {
           //listening Music
            ListeningMusic();
           // keeping charge to iphone
            ChargingIphone();
        }
        public static void ListeningMusic()
        {
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine("Listening Song "+i);
            }
        }
        public static void ChargingIphone()
        {
            for (int i = 0; i < 11; i++)
            {
                Console.WriteLine("Charging done " + i*10 +" %");
            }
            Console.WriteLine("100 %  charging please unplug the charger");
        }
    }

As you see in above code , we have created both normal functions and that we will execute, So let’s run it and see the output.

Output:

Listening Song 0
Listening Song 1
Listening Song 2
Listening Song 3
Listening Song 4
Listening Song 5
Charging done 0 %
Charging done 10 %
Charging done 20 %
Charging done 30 %
Charging done 40 %
Charging done 50 %
Charging done 60 %
Charging done 70 %
Charging done 80 %
Charging done 90 %
Charging done 100 %
100 % charging please unplug the charger

If you see both functions / methods ran synchronously i.e one after the other. Here ChargingIphone 2 have to wait till ListeningMusic has finished his loop.

Asynchronous way means where multiple jobs executed simultaneously. It is similar to multitasking. No dependency on another work completion in asynchronous. This is also called as Asynchronous Threading.

Work in Process
work1-->execution
work2-- execution

For Example, Listening music and keeping charging for a mobile simultaneously is most common example for asynchronous threading.

It will take more time to execute the works one after another.

Now to do things faster we need a mechanism which executes the task simultaneously. so, To make that C# has a special mechanism called Multi threading.

C# Threading Example with simultaneous execution

Code

class Iphone
{
static void Main(string[] args)
{
Console.WriteLine("Main Thread begins");

//creating thread objects
Thread th1 = new Thread(ListeningMusic);
Thread th2 = new Thread(ChargingIphone);

//invoking Threading objects.
// thread to start listening music task
th1.Start();

//thread to start to phone charging.
th2.Start();
Console.WriteLine("Main Thread terminates");
}
public static void ListeningMusic()
{
for (int i = 0; i < 6; i++)
{
Console.WriteLine("Listening Song " + i);
}
}
public static void ChargingIphone()
{
for (int i = 0; i < 11; i++)
{
Console.WriteLine("Charging done " + i * 10 + " %");
}
Console.WriteLine("100 % charging please unplug the charger");
}
}

As you see we have invoked Thread objects successfully. Now let’s run this program to see the Output.

Output

Main Thread begins
Listening Song 0
Listening Song 1
Charging done 0 %
Charging done 10 %
Charging done 20 %
Charging done 30 %
Listening Song 2
Listening Song 3
Charging done 40 %
Charging done 50 %
Charging done 60 %
Charging done 70 %
Charging done 80 %
Listening Song 4
Listening Song 5
Charging done 90 %
Charging done 100 %
100 % charging please unplug the charger
Main Thread terminates

As you see in output that ListeningMusic method is also simultaneously executing with ChargingIphone method it means both methods are working asynchronously.

So as per above examples it is been concluded that using threading we can execute multiple work in a asynchronously.

Related Posts