What is Synchronous? || Write the full details of Synchronous with example

What is Synchronous? || Write the full details of Synchronous with example

What is Synchronous?

In computing, Synchronous refers to a method of executing tasks or operations sequentially, where each task waits for the previous one to finish before starting. In other words, the tasks are performed one after another in a blocking manner.

When a function or process is synchronous, it blocks the execution of further code until it completes. This is simple to understand and debug but can lead to inefficiencies, especially when dealing with slow operations like file reading or network requests.


Key Characteristics of Synchronous Execution:

FeatureDescription
OrderExecutes tasks one at a time in sequence
BlockingEach task waits for the previous to finish
Simple flowEasy to understand and debug
Slower performanceTime-consuming operations block others
No callbacks or promisesDoesn’t require handling for deferred results

Real-World Analogy:

Imagine you’re at a bank counter and there’s only one cashier. Each person stands in line and waits for the previous customer’s transaction to finish before they can be served.


Synchronous Flow Example in Daily Life:

🍽️ Cooking a meal synchronously:

  1. Boil water
  2. Wait for it to finish
  3. Cook rice
  4. Wait for it to finish
  5. Fry vegetables
  6. Wait for it to finish

Only after finishing one step do you move to the next. Each step blocks the next one.


Example in Programming (Synchronous JavaScript)

function task1() {
  console.log("Task 1: Start");
  // Simulate some process
  for (let i = 0; i < 1000000000; i++) {}
  console.log("Task 1: End");
}

function task2() {
  console.log("Task 2: Start and End");
}

task1();
task2();

Output:

Task 1: Start
Task 1: End
Task 2: Start and End

Explanation:

  • task2() waits for task1() to complete before it starts.
  • Even though task2() is simple, it cannot run until task1() finishes.
  • This is synchronous execution — step-by-step, blocking.

Synchronous in Other Languages

Python Example:

import time

def task1():
    print("Task 1 starting...")
    time.sleep(2)
    print("Task 1 finished.")

def task2():
    print("Task 2 starting and finishing.")

task1()
task2()

Output:

Task 1 starting...
(wait 2 seconds)
Task 1 finished.
Task 2 starting and finishing.

Again, task2() has to wait until task1() is completely finished.


When is Synchronous Useful?

Use CaseWhy it’s useful
Simple scripts or batch jobsEasy to manage step-by-step logic
File reading/writingEnsures order and consistency
Small applicationsNo need for complex async logic
Debugging and learningEasier to follow the flow

Disadvantages of Synchronous Execution:

  • Slower Performance: Long operations block everything else.
  • Inefficiency: Wastes time waiting for tasks like disk or network I/O.
  • Bad for User Experience: In GUI apps, synchronous actions can freeze the UI.

Comparison: Synchronous vs Asynchronous

FeatureSynchronousAsynchronous
Execution OrderOne after another (blocking)Can run concurrently (non-blocking)
SpeedSlowerFaster (non-blocking I/O)
Code SimplicitySimple and linearMore complex (callbacks/promises)
ExampleTraditional function callsAJAX, fetch(), threading

Visual Comparison:

Synchronous Flow:

Task 1 ----------->
Task 2 ----------->
Task 3 ----------->

Asynchronous Flow:

Task 1 ------->     (starts)
Task 2 ------->     (starts before Task 1 finishes)
Task 3 ------->     (starts before others finish)

Summary

AspectDetails
MeaningTasks run one after the other
TypeBlocking
Easy to use?Yes, especially for beginners
Suitable forSmall programs, sequential operations
Not good forNetwork apps, real-time systems

Conclusion:

Synchronous programming is simple, clear, and easy to follow, but it’s not always the most efficient for tasks that involve waiting, such as API calls, file access, or databases. In such cases, asynchronous programming is often a better choice.

Still, for many scenarios, especially in scripting, automation, and basic programming, synchronous execution is perfectly suitable and recommended for simplicity and readability.


Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *