FOLASAYO SAMUEL OLAYEMI
SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

What distinguishes synchronous from asynchronous threads in C#?

FOLASAYO SAMUEL OLAYEMI's photo
FOLASAYO SAMUEL OLAYEMI
·Sep 8, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • What is synchronized, and how does C# use it?
  • What is asynchronous programming and how does C# use it?
  • Is Asynchronous Programming Better Than Synchronous Programming?
  • When does a software not require asynchronous processing?

What is synchronized, and how does C# use it?

Methods in C# always run synchronously by default. One synchronous operation task at a time, and only after that task is finished is the next unblocked. In other words, you must wait for a task to be completed before going on to the next.

Code Sample:

using System;

namespace CSharpSynchronousExample
{
    class Program
    {
        public static void Main(string[]args)
        {
            Console.WriteLine("OUTPUT");

            GetStudentName();
            GetStudentNumber();
            Console.ReadLine();
        }

        private static void GetStudentName()
        {
            Task.Delay(1000);
            Console.WriteLine("Student Name is Samuel");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "1";

            Thread currentThread = Thread.CurrentThread;
            Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }

        private static void GetStudentNumber()
        {
            Task.Delay(1000);
            Console.WriteLine("Student Number is 12");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "2";

        Thread currentThread = Thread.CurrentThread;
        Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }
    }
}

Output

AC.png

What is asynchronous programming and how does C# use it?

On the other hand, using asynchronous operations, you can switch to a new task before the previous one is complete. This allows you to handle several requests concurrently and finish more work in a shorter amount of time thanks to asynchronous programming.

Asynchronous programming is frequently associated with parallelization, which is the practice of carrying out separate activities in parallel utilizing, you guessed it, asynchronous programming.

What is typically processed sequentially can be divided into smaller units that can run independently and concurrently using parallelization. In addition to methods and capabilities, parallelization has to do with how systems and software are made.

Applying parallelization concepts can enable you to get results much more quickly and efficiently.

Code Sample

using System;

namespace CSharpSynchronousExample
{
    // Async Await example
    class Program
    {
        public static async Task Main(string[]args)
        {
            Console.WriteLine("OUTPUT");

            await GetStudentName();
            await GetStudentNumber();
            Console.WriteLine("All async Tasks run successfully");
            Console.ReadLine();
        }

        private static async Task GetStudentName()
        {
            await Task.Delay(1000);
            Console.WriteLine("Student Name is Samuel");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "1";

            Thread currentThread = Thread.CurrentThread;
            Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }

        private static async Task GetStudentNumber()
        {
            await Task.Delay(1000);
            Console.WriteLine("Student Number is 12");

            if(Thread.CurrentThread.Name == null)
                Thread.CurrentThread.Name = "2";

        Thread currentThread = Thread.CurrentThread;
        Console.WriteLine("Thread details " + currentThread.Name.ToString());
        }
    }
}

Output

AS.png

Is Asynchronous Programming Better Than Synchronous Programming?

Simply put: No. Although there are many advantages, not all processes should execute asynchronously and in parallel. Let's examine when asynchronous programming is appropriate and when sticking with synchronous execution is the best choice.

When does a software not require asynchronous processing?

Asynchronous programming is only appropriate when dealing with independent tasks.

  • A micro-service is constantly barraged with requests and has very little CPU-bound operations. Async is therefore not required.

  • A database that cannot scale is being called by a service. Making the code async won't assist in this situation because the database will become a bottleneck.

  • If an application contains I/O-bound task that is extremely sluggish but does not get a lot of requests, it does not need to scale.

  • a monolithic application that performs numerous CPU-bound computations, each of which is dependent on the computation before it.

Thanks for reading...

Happy Coding!

Did you find this article valuable?

Support FOLASAYO SAMUEL OLAYEMI by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this