Synchronous & Asynchronous and MultiThread Programming -1 — The concept of Thread

Alperen Öz
5 min readApr 22, 2024

--

The concept of Thread

  • Threads are responsible for executing specific tasks within a program, much like individual methods.
  • These threads, or program segments, can either complete their task and terminate or continue to run as long as the program is active.
  • When a software is launched or executed in an operating system, a thread is created as the initial task without requiring user intervention.
  • This thread is commonly referred to as the main thread.
  • While the main thread controls its own flow, it also manages the execution and flow of other smaller program segments, known as threads.
  • These additional threads, typically referred to as worker threads, are created by the user to perform specific tasks concurrently or simultaneously, allowing certain tasks to be performed simultaneously or in parallel.
  • The termination of these additional threads does not affect the program flow, but the termination of the main thread signifies the end of the program.
  • For example, in a Windows Form project, closing the form can be considered.

Asynchronous — Synchronous Programming Concepts

  • Asynchronous operation refers to the situation where the execution of a task, or the completion of its operation, is not dependent on the start or finish of any other task.
  • In other words, asynchronous operation allows other tasks to continue without being blocked while one task is ongoing.
  • An example of asynchronous operation can be illustrated through email correspondence. When sending an email to someone, the sender can continue to perform other tasks or correspond with others without waiting for a response from the recipient.
  • Synchronous operation signifies a scenario where the start of one task is dependent on the completion of another task.
  • At a given time ‘T’, only one task can be executed in synchronous operation; multiple tasks cannot be executed simultaneously during this time.
  • To exemplify synchronous operation, consider a person conversing on the phone with someone else. During a phone call, the person cannot engage in another phone conversation until the ongoing call is finished.
  • In summary, when a task, process, or code is executed synchronously, other tasks must wait for the completion of that task before they can proceed. However, with asynchronous execution, other tasks can be initiated without waiting for the completion of the current task.

Synchronous and Asynchronous operation can be achieved with either a single thread or multiple threads.

Synchronous — Single Thread

Source: Gençay Yıldız — Asenkron & Multithread Programlama

Synchronous— Multi Thread

Source: Gençay Yıldız — Asenkron & Multithread Programlama

Asynchronous— Single Thread

Source: Gençay Yıldız — Asenkron & Multithread Programlama

Asynchronous— Multi Thread

Source: Gençay Yıldız — Asenkron & Multithread Programlama

Differences Between Asynchronous — Synchronous Programming and MultiThread Programming

  • In essence, both asynchronous and synchronous programming, as well as multithread programming, serve the same purpose. This purpose is to execute different processes in parallel without unnecessarily blocking or waiting for each other in the written code.
  • However, despite having the same goal, these two concepts and modes of operation differ from each other.

In Asynchronous Programming

  • Asynchronous programming is a model where processes in the code flow independently of each other. There is no need to wait for one process to finish for another one to start.
  • As mentioned earlier, asynchronous code can be written with one or more threads. The main focus is to ensure that the code runs without blocking the thread it operates on.
  • It generally involves lower memory usage.

In MultiThread Programming

  • Multithread programming is a model where a code flow operates on multiple threads simultaneously.
  • It directly relies on a thread-based approach.
  • Since each thread requires a certain amount of memory, it is more costly in terms of memory usage.

To summarize with an example, in a restaurant with only one chef, orders are prepared without waiting for each other, representing an example of asynchronous programming. On the other hand, in a restaurant with multiple chefs, each chef handles different orders independently, representing an example of multithread programming. And each chef can share the same kitchen equipment. Here, in both examples, chefs correspond to threads.

When to Use Asynchronous, When to Use Multithreading?

While both asynchronous and multithread programming serve the same purpose, sometimes a choice needs to be made between the two depending on the requirement.

In terms of Improving Wait Times

  • Asynchronous Programming can effectively manage wait times, such as long file I/O operations, network calls, etc., in your application. It helps in managing these wait times efficiently.
  • Multithreading can be used when your application involves CPU-intensive tasks, and executing these tasks in parallel enhances performance.

In terms of Concurrency Requirements

  • Asynchronous Programming can effectively manage concurrent tasks in your application, such as in a web application that hosts many concurrent tasks.
  • Multithreading is useful when your application requires a large number of concurrent threads to operate simultaneously.

In terms of Security and Synchronization

  • If you prefer not to deal with synchronization issues and desire a simpler model, you can opt for asynchronous programming.
  • In Multithreading, if you are willing to address synchronization issues between parallel threads and ensure secure access to shared resources, multithread programming can be used.

Task and Thread Concepts

TASK

  • The Task class, part of the Task Parallel Library (TPL), is a C# class that enables parallel programming and asynchronous operations.
  • The Task class can execute threads in a thread pool managed by .NET, benefiting from mechanisms like the garbage collector.
  • Through TPL, the Task class provides concurrency and parallelism. It can manage and execute multiple tasks concurrently.
  • With keywords like async and await, Task class usage is straightforward. Methods like WhenAll and WhenAny make it easy to wait for and manage the completion of multiple tasks.

THREAD

  • The Thread class aims to provide a lower-level abstraction compared to Task, focusing on directly creating and managing threads. It facilitates direct implementation of multithreading. In projects and applications, the Thread class requires more responsibility and attention compared to Task.
  • The Thread class executes threads directly on a thread created by the operating system, potentially exposing more to unmanaged resources outside of .NET, leading to increased system resource consumption.
  • Thread class typically aims to perform parallel operations by directly creating threads.

In summary, the Task class is more modern and offers a more straightforward approach compared to Thread. However, the Thread class provides more control and flexibility, albeit with greater responsibility and attention required. Depending on the scenario and requirement, both classes and approaches should be utilized.

C# programming language is inherently synchronous. This means that written and called code blocks will execute sequentially.

Source: Gençay Yıldız — Asenkron & Multithread Programlama

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Alperen Öz
Alperen Öz

Written by Alperen Öz

Software Engineer @SmartPulse

No responses yet

Write a response