WTF is Synchronous and Asynchronous!

Skrew Everything
From The Scratch
Published in
3 min readMar 23, 2018

Synchronous Execution:

Synchronous, or Synchronized means “connected”, or “dependent” in some way.

When you execute something synchronously, you wait for it to finish before moving on to another task.

In other words, Synchronous execution means the execution happens in a single series. A->B->C->D. If you are calling those routines, A will run, then finish, then B will start, then finish, then C will start, etc.

Synchronization refers to two or more processes’ start and end points, NOT their executions. In this example, Process A’s endpoint is synchronized with Process B’s start point:

SYNCHRONOUS
|--------A--------|
|--------B--------|

When a task is executed synchronously, you wait for a task to be completed before moving on to another task. One task depends on the end of another.

Synchronous (one thread):

1 thread ->   |<---A---->||<----B---------->||<------C----->|

Synchronous (multi-threaded):

thread A -> |<---A---->|   
\
thread B ------------> ->|<----B---------->|
\
thread C ----------------------------------> ->|<------C----->|

Asynchronous Execution:

When you execute something asynchronously, you can move on to another task before it finishes.

Asynchronous processes, on the other hand, do not have their start and endpoints synchronized:

ASYNCHRONOUS
|--------A--------|
|--------B--------|

Where Process A overlaps Process B, they’re running concurrently or synchronously (dictionary definition).

When a task is executed asynchronously, you can directly switch to another task before the previous has been completed. One task does not depend on the other.

In programming, tasks are executed on a “thread”. It is necessary to understand that several threads can be created for tasks in order to avoid stacking in a single queue.

That being said, the concept of synchronous and asynchronous do not have much to do with threads, the reason I mention it is that it is rare to see tasks running asynchronously on a single thread (although technically possible).

Asynchronous (one thread):

Asynchronous (multi-Threaded):

thread A ->     |<---A---->|
thread B -----> |<----B---------->|
thread C ---------> |<------C--------->|
  • Start and end points of tasks A, B, C represented by <, > characters.
  • CPU time slices represented by vertical bars |

In simpler terms:

SYNCHRONOUS

You are in a queue to get a movie ticket. You cannot get one until everybody in front of you gets one, and the same applies to the people queued behind you.

ASYNCHRONOUS

You are in a restaurant with many other people. You order your food. Other people can also order their food, they don’t have to wait for your food to be cooked and served to you before they can order. In the kitchen restaurant workers are continuously cooking, serving, and taking orders. People will get their food served as soon as it is cooked.

Common Confusion:

When you look at the dictionary, you might get confused. And this might be your question,

What absolutely confuses me is that synchronous means “at the same time”, yet when used in the sense above, it means sequential, and asynchronous means “not at the same time”…??

Oddly enough “Synchronously” means “using the same clock” so when two instructions are synchronous they use the same clock and must happen one after the other. “Asynchronous” means “not using the same clock” so the instructions are not concerned with being in step with each other. That’s why it looks backwards, the term is not referring to the instructions relationship to each other. It’s referring to each instructions relationship to the clock.

The terms come from engineering. en.wikipedia.org/wiki/Asynchronous_system

Got any doubts/questions/suggestions? Comment down below.

--

--

Skrew Everything
From The Scratch

A wannabe artist 👨‍🎨, but can’t draw 😫. A wannabe athlete 🏃‍♂️,but can’t run 🥵.Found my peace with coding 👨‍💻 and writing ✍️. Twitter.com/SkrewEverything