1361

What is the difference between asynchronous and synchronous execution?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
tush1r
  • 19,443
  • 14
  • 36
  • 35
  • ... or "sequential vs parallel" execution ... although true parallelism requires multiple cores, otherwise it's logically interleaved. – samus May 30 '18 at 16:36
  • 12
    I used to confuse both terms, and the way I did to remember the difference is throw the first "A" in "**A**JAX" which stands for asynchronous, in JavaScript when you do requests with AJAX in a loop they don't wait for each others or block the process, because the browser doesn't want to impact the user experience with a frozen website, all requests are sent almost at the same time without waiting the response of the previous request. That is **asynchronous** – Accountant م Jan 15 '19 at 10:19
  • 1
    Well, I don't think the answers explain the original motivation for the terminology. But here's my take from what I came to know so far: synchronous - act based on a point of time, like the end of a timeout. asynchronous - act based on an event happening, irrespective of time These terms make a lot of sense when put this way. However, the meaning of 'synchronous' is being twisted in this particular context. – nomad Sep 04 '20 at 20:57
  • 1
    this question is [discussed at meta](https://meta.stackoverflow.com/q/420519/839601) – gnat Sep 26 '22 at 07:18

22 Answers22

1944

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

In the context of operating systems, this corresponds to executing a process or task on a "thread." A thread is a series of commands (a block of code) that exist as a unit of work. The operating system runs a given thread on a processor core. However, a processor core can only execute a single thread at once. It has no concept of running multiple threads simultaneously. The operating system can provide the illusion of running multiple threads at once by running each thread for a small slice of time (such as 1ms), and continuously switching between threads.

Now, if you introduce multiple processor cores into the mix, then threads CAN execute at the same time. The operating system can allocate time to one thread on the first processor core, then allocate the same block of time to another thread on a different processor core. All of this is about allowing the operating system to manage the completion of your task while you can go on in your code and do other things.

Asynchronous programming is a complicated topic because of the semantics of how things tie together when you can do them at the same time. There are numerous articles and books on the subject; have a look!

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 1
    Perhaps also worth mentioning that the threads may be in the kernel, with user-space taking advantage of them with a single thread using non-blocking mechanisms like select(2) or asynchronous mechanisms like I/O completion ports. – Jean-Paul Calderone Jun 17 '13 at 19:33
  • 305
    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"...?? Can somebody explain this conflict? – Damien Roche Aug 07 '13 at 17:53
  • 55
    @Zenph: In this context, an entire block of code is what we're concerned with. Synchronous means that the block is executed at the same time (though, yes, the components are executed sequentially). Asynchronous means that the block is not all executed at the same time. – Adam Robinson Aug 07 '13 at 18:15
  • Is task generally a thread? – committedandroider Dec 01 '14 at 01:23
  • 1
    @committedandroider: A `Task` generally takes place on a thread (though some things like file and network operations would also make use of IO completion threads, which are different) but is almost never a thread itself. By default, the threads used to execute tasks come from the thread pool; they are not spun up individually for each task and shut down upon its completion. – Adam Robinson Dec 01 '14 at 14:18
  • 13
    Asynchronous execution also happens when a program sends a message to a queue (as in messaging systems, such as ActiveMQ, WebSphere MQ, HornetQ, MSMQ, etc.). In this case, the asynchronous call doesn't involve multithread programming or handling concurrency at the OS level. – Paulo Merson Jun 15 '15 at 12:07
  • 342
    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. Hope that helps. – Tom Padilla Jan 28 '16 at 15:31
  • 16
    The terms come from engineering. https://en.wikipedia.org/wiki/Asynchronous_system – Tom Padilla Jan 28 '16 at 15:50
  • 4
    Actually, "Synchronized" means "connected", or "dependent" in some way. in other words the two tasks must be aware of one another, and one must execute in some way that is dependent on the other. In mist cases that means that one cannot start until the other has completed. Asynchronous means they are totally independent and neither one must consider the other in any way in initiation or in execution. – Charles Bretana Mar 16 '16 at 17:26
  • 3
    Asynchronity doesn't automatically translate into executing code on another thread. For example the WPF message pump allows to perform asynchronous operations on one single thread. – Dennis Kassel Jun 02 '16 at 13:49
  • 1
    Thanks for the multiple attempts to explain the confusing apparent opposite meaning of the terms. Yet it will take some more pondering for me to get convinced that they're as explained above, not the opposite. – Sam Sirry Feb 21 '17 at 10:19
  • 2
    @AdamRobinson, synchronous/asynchronous doesn't have to do with multi-threading. JavaScript is well asynchronous and yet single-threaded. In any case, multi-threading is a way to implement asynchronous processing, but not the only one. – dRamentol May 03 '17 at 18:15
  • Excellent, perfect answer. Indeed, it is very tricky to get two threads to work together properly on a mid-level of the stack. For example, I am using Delphi's `TThread` predominantly for multi-threading. It does a great job of implementing threads, where they get split by the OS on different cores automagically. But, any developer must still really pay close attention to how each thread interacts with each other. It's not always a trivial task. I've come to rely on event-driven threads. – Jerry Dodge May 09 '17 at 00:02
  • 2
    (a)synchronicity is not at all inherently related to threads. I think this is very misleading. also, see: https://blog.stephencleary.com/2013/11/there-is-no-thread.html – sara Sep 20 '17 at 15:53
  • 2
    @DamienRoche synchronous means "at the same time **line**" – Accountant م Jan 15 '19 at 10:26
  • 2
    @DamienRoche You can blame the trappings of English traps, my friend. It’s all too common to mix up **a**word with **anti**word. It doesn’t help that many native English speakers mix these up too. Going by the synchronous definition you provided, the crux here is that **a**synchronous has no concept or respect for time, ergo it isn’t synchronous by its very nature. – kayleeFrye_onDeck Jan 31 '19 at 05:47
  • @TomPadilla thank you for your clarity. I knew I had two concepts in my head that needed reconciliation. It was more ironic because not being dependent on a clock in asynchronous communication in engineering (using handshakes, etc) looks remarkably like synchronous communication in computer science.. I still feel as if computer science should have used a different term. – Gerard ONeill May 08 '19 at 23:52
1280

Synchronous/Asynchronous HAS NOTHING TO DO WITH MULTI-THREADING.

Synchronous or Synchronized means "connected", or "dependent" in some way. In other words, two synchronous tasks must be aware of one another, and one task must execute in some way that is dependent on the other, such as wait to start until the other task has completed.
Asynchronous means they are totally independent and neither one must consider the other in any way, either in the initiation or in execution.

Synchronous (one thread):

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

Synchronous (multi-threaded):

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

Asynchronous (one thread):

         A-Start ------------------------------------------ A-End   
           | B-Start -----------------------------------------|--- B-End   
           |    |      C-Start ------------------- C-End      |      |   
           |    |       |                           |         |      |
           V    V       V                           V         V      V      
1 thread->|<-A-|<--B---|<-C-|-A-|-C-|--A--|-B-|--C-->|---A---->|--B-->| 

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 |

Technically, the concept of synchronous/asynchronous really does not have anything to do with threads. Although, in general, it is unusual to find asynchronous tasks running on the same thread, it is possible, (see below for examples) and it is common to find two or more tasks executing synchronously on separate threads... No, the concept of synchronous/asynchronous has to do solely with whether or not a second or subsequent task can be initiated before the other (first) task has completed, or whether it must wait. That is all. What thread (or threads), or processes, or CPUs, or indeed, what hardware, the task[s] are executed on is not relevant. Indeed, to make this point I have edited the graphics to show this.


ASYNCHRONOUS EXAMPLE:

In solving many engineering problems, the software is designed to split up the overall problem into multiple individual tasks and then execute them asynchronously. Inverting a matrix, or a finite element analysis problem, are good examples. In computing, sorting a list is an example. The quicksort routine, for example, splits the list into two lists and performs a quicksort on each of them, calling itself (quicksort) recursively. In both of the above examples, the two tasks can (and often were) executed asynchronously. They do not need to be on separate threads. Even a machine with one CPU and only one thread of execution can be coded to initiate processing of a second task before the first one has completed. The only criterion is that the results of one task are not necessary as inputs to the other task. As long as the start and end times of the tasks overlap, (possible only if the output of neither is needed as inputs to the other), they are being executed asynchronously, no matter how many threads are in use.

SYNCHRONOUS EXAMPLE:

Any process consisting of multiple tasks where the tasks must be executed in sequence, but one must be executed on another machine (Fetch and/or update data, get a stock quote from financial service, etc.). If it's on a separate machine it is on a separate thread, whether synchronous or asynchronous.

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • 112
    why in the world do words mean different things in computer...always leave me coming back to this...from dictionary.. synchronous: **occurring at the same time.** asynchronous: **not occurring at the same time.** – Muhammad Umer Sep 08 '13 at 14:56
  • 18
    but as you can see in computers it means the opposite – Muhammad Umer Sep 08 '13 at 14:57
  • 6
    Maybe the nomenclature is based on whether initiation of tasks is "synchronized" with completion of other tasks? – Charles Bretana Sep 18 '13 at 16:10
  • 15
    @MuhammadUmer: in computer world, occurring at the same time is called concurrency. – Roy Ling Oct 18 '13 at 01:12
  • @Muhammad, here some good answers on SO: http://stackoverflow.com/questions/7131991/asynchronous-and-synchronous-terms http://stackoverflow.com/questions/1596589/why-are-asynchronous-processes-not-called-synchronous – apokaliptis Dec 24 '13 at 05:01
  • Reversed edit, Asynchronous means there is only ONE logical thread of execution, so there can be NO overlap. Not even for one cpu clock cycle. The start point of task two MUST be after the end of task one. – Charles Bretana Apr 03 '14 at 14:55
  • Synchronous because operation A is still running while the remote procedure (disk write, network fetch...) completes; asynchronous because it stops. – Alice Purcell Jul 29 '14 at 12:39
  • 6
    IMHO, these pictures do not all describe the synchronous vs asycnhronous execution of tasks For example, the second picture implies that asynchronous tasks require several threads. Actually, it does not. And that tasks have to run in parallel, which is not a requirement as well. Or, the picture for "synchronous" very well shows how tasks have been asynchronously dispatched form some call-site and now execute in a serial task scheduler ;) IMO, the pictures are misleading. – CouchDeveloper Sep 15 '15 at 07:35
  • 2
    Actually, CouchDeveloper, the picture implies nothing about threads at all. It simply implies, or represents, that when multiple tasks are executed asynchronously, the second and subsequent tasks can begin before the preceding ones have finished, whereas when they are executed synchronously, they have to wait until all preceding tasks have completed. Whether they are on different threads (or different processes), on the same machine, on different machines, or on the same thread on a single CPU machine, where the thread is constantly switching between one task and the others, is irrelevant. – Charles Bretana Sep 15 '15 at 14:34
  • 1
    @CharlesBretana I suppose concurrency cannot be achieved with synchronous execution and this(concurrency) can be done only submitting work items asynchronously. If you feel it might be relevant for other readers, please edit your response and shed some light regarding this topic as I feel they are all closely related. – bibscy Dec 02 '16 at 13:06
  • 1
    @bibscy, I'm not sure what you mean by "concurrency cannot be achieved with synchronous execution". If you understand my post, then this statement is tautological. Obviously, if one task is dependent on the results of another, (the definition of synchronous), then they cannot be run concurrently. And if two tasks must be synchronized, then of course, you cannot "submit them asynchronously" Where is the dependent one getting the data it needs from completion of the other one? – Charles Bretana Dec 02 '16 at 14:30
  • @MuhammadUmer - I came here with the same question. Synchronous means 'at the same time', of course, not any other redefinition just for computers, and the two things that are as nearly at the same time as possible are the beginning and end of a task. Per the boss picture above, the worker can't do anything else till they are done with the task, so they effectively are putting nothing in between beginning the work and completing it - so, completing it *synchronously*. Hope that helps! – Dean Radcliffe Dec 28 '16 at 15:25
  • 1
    @Dean, in this context Synchronous does not mean "at the same time", it means "synchronized with" as in it must be executed or performed "in coordination" with or "in conjunction" with, the other task. "Concurrent": means "at the same time". Concurrent and Synchronous are totally different concepts. "Synchronizing" one task to run "at the same time" as another is not the *only* way a task can be "synchronized" with another. A Task can be "synchronized" to run 12 hours after another. That too would be synchronized. – Charles Bretana Dec 28 '16 at 16:12
  • @CharlesBretana, again confusing synchronous/asynchronous processing with multi-threading. – dRamentol May 03 '17 at 18:18
  • So glad I found this answer. I have been confused about this for a very long time, and had almost given up all hopes of ever understanding it. Now, it has all started making sense, thanks to your emphasis on "synchronous/asynchronous really does not have anything to do with threads". – Pradeep Puranik Nov 03 '17 at 18:22
  • 1
    @CharlesBretana, thanks for this answer. You say...'Even a machine with one CPU, and only one thread of execution can be coded to initiate processing of a second task before a first one has completed.', Can you please name some programming constructs which help in achieving this? – Mandroid Mar 26 '18 at 06:42
  • 2
    @Mandroid, Sure, look at early Unix Operating system running on 8086 or 8088 processors. They had only one CPU, so, by definition, they had only one physical thread, and yet it was a multi-Processing OS. (although within the OS code, it created multiple "logical" threads to organize the multiple tasks they were executing.) But even on only one logical thread, I could code a procedural program that interspersed instructions to perform one task with other instructions that performed a second task - even one completely unrelated to the first one. – Charles Bretana Mar 26 '18 at 15:32
  • 2
    @Mandroid, A common example is in recursive engineering problems, like inverting a matrix, or a Finite Element Analysis problem. In these software algorithms, the overall routine divides the problem into multiple smaller pieces, each of which can be solved independently of the others. The overall routine sets up, initiates and executes the multiple pieces in an overlapping fashion, like my diagram above labeled *Asynchronous (one thread)*. Sometimes the results must be combined to generate the overall result of the process, and sometimes not. – Charles Bretana Mar 26 '18 at 15:43
  • 2
    @Mandroid: A typical example might be "threads" in python (`threading` package). They allow you to do work _concurrently_---in a given _interval_, progress can be made on multiple tasks. But at a given _instant_ in time, only one task is actually being operated on (so you can't take advantage of multiple CPUs). The key idea here is "interruptibility"---work can be halted on one thread while it's performed on another. Useful in Python for I/O-bound tasks but not CPU-bound tasks. – user1071847 Mar 27 '18 at 11:28
  • 2
    Most of us here (including myself) probably better understand things visually, so +1 for the ASCII art charts :D – hewiefreeman Aug 26 '18 at 20:44
  • "Asynchronous means they are totally independent & neither one must consider the other in any way, either in initiation or in execution." Above quote is too strict, narrow, & conflicts with the definition of Asynchronous System & Asynchronous Circuit on Wikipedia. OP didn't use the term "multi-threading." Event-loop languages have 'sync' & 'async' functions. Former must COMPLETE work before next function begins. Latter requires a call-back-function parameter, & immediate execution of a subsequent function begins, but it's work is not complete yet, & when it is, the call-back will be fired. – Jaycephus Oct 16 '18 at 23:28
  • Jaycephus, Your objections are dependent on how you define "next function". In a routine that inverts a matrix, (or, for another example, the quicksort algorithm), where the main algorithm recursively divides the problem into multiple smaller problems, is that synchronous or asynchronous? Which problem or problems is/are the "next" one? Which is/are the "callback"? – Charles Bretana Jul 22 '19 at 11:25
  • amazing explanation, thank you. Asynchronous (one thread) and Synchronous (one thread) example is here https://github.com/pavoltravnik/async – Pavol Travnik May 27 '20 at 11:22
  • So JavaScript is an asynchronus, single threaded language then? – cham Jul 15 '20 at 21:16
  • 2
    No. It is silly to apply the terms synchronous/asynchronous, or single/multi-threaded to a programming language. You can write a software algorithm in any language that will execute in any one of these four modes, (depending on the OS and hardware it is executing on. – Charles Bretana Jul 16 '20 at 00:24
722

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.

Ryan
  • 993
  • 1
  • 9
  • 17
themightysapien
  • 8,309
  • 2
  • 17
  • 15
  • 18
    If someone wants apples compared with apples; if you wanted the restaurant scenario to be synchronous, then when you order food, everyone else in the restaurant would have to wait for your food to arrive before they can order their food etc. Now this seems like a really dumb scenario to be in, but in the computing world this scenario could be useful. Say each customer cant decide what they want, and instead want to look at what the previous customer orders to decide if they want that or not, then it makes sense that they have to wait for the food to arrive before ordering. – Fonix Feb 01 '17 at 03:42
  • Just to add on... it could be so that the operations execute like in a queue in Asynchronous operations... But that is not mandatory at all. – Sreekanth Karumanaghat Sep 13 '17 at 13:05
  • 4
    To push your example even further, they could consider several gates for selling tickets. Therefore, each line can work asynchronously from the other line but synchronously within itself! – Saeed Ahadian Feb 22 '20 at 11:08
407

Simple Explanation via analogy

Angry employee

(story & pics given to help you remember).

Synchronous Execution

My boss is a busy man. He tells me to write code. I tell him: Fine. I get started and he's watching me like a vulture, standing behind me, off my shoulder. I'm like "Dude, WTF: why don't you go and do something while I finish this?"

he's like: "No, I'm waiting right here until you finish." This is synchronous.

Asynchronous Execution

The boss tells me to do it, and rather than waiting right there for my work, the boss goes off and does other tasks. When I finish my job I simply report to my boss and say: "I'm DONE!" This is Asynchronous Execution.

(Take my advice: NEVER work with the boss behind you.)

BenKoshy
  • 33,477
  • 14
  • 111
  • 80
97

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.

With Asynchronous execution, you begin a routine, and let it run in the background while you start your next, then at some point, say "wait for this to finish". It's more like:

Start A->B->C->D->Wait for A to finish

The advantage is that you can execute B, C, and or D while A is still running (in the background, on a separate thread), so you can take better advantage of your resources and have fewer "hangs" or "waits".

Jimi
  • 901
  • 7
  • 11
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • @ Reed Copsey ...... Thanks for such a good explanation ..... Just wanted some more info on Async-Exec ...... Based on your answer in Async Exec .... Start A->B->C->D->Wait for A to finish ... So all A,B, C, D starts at a time ...... and they wait for A to finish ..... So does B will only finish after A finishes , and C after B and so on ...... ? Or can B first finish and then A can finish ? – Devrath Jul 20 '13 at 16:34
  • 9
    @Devrath The operations can finish in any order. – Reed Copsey Jul 22 '13 at 15:45
65

In a nutshell, 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--------|

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), hence the confusion.

UPDATE: Charles Bretana improved his answer, so this answer is now just a simple (potentially oversimplified) mnemonic.

entr0p3te
  • 659
  • 5
  • 4
  • 3
    copy of Charles Bretana answer – Dinesh Saini Aug 18 '15 at 04:37
  • 3
    @DineshSaini - My diagram is slightly different . For clarity, I placed A on top of B in both cases, emphasizing whether their start & endpoints are synchronized. Charles Bretana's diagram places the synchronous processes in sequence without "syncing" anything. (I was going to comment below his answer to "improve" it, but realized it would be easier just to show the new diagram.) – entr0p3te Aug 19 '15 at 14:00
  • Great diagrams. I think the way to call the top one SYNC, is that the start and end of A in the top diagram are effectively at the same time, in the sense that no other events have intervened, or could have interfered with A's completion. Sync can refer to a single task in isolation, like adding to CPU registers, whose start and end are so close, as to effectively be actually dictionary-synchronous. – Dean Radcliffe Dec 28 '16 at 15:32
61

Synchronous means that the caller waits for the response or completion, asynchronous that the caller continues and a response comes later (if applicable).

As an example:

static void Main(string[] args)
{
    Console.WriteLine("Before call");
    doSomething();
    Console.WriteLine("After call");
}

private static void doSomething()
{
    Console.WriteLine("In call");
}

This will always ouput:

Before call
In call
After call

But if we were to make doSomething() asynchronous (multiple ways to do it), then the output could become:

Before call
After call
In call

Because the method making the asynchronous call would immediately continue with the next line of code. I say "could", because order of execution can't be guaranteed with asynch operations. It could also execute as the original, depending on thread timings, etc.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Ragoczy
  • 2,907
  • 1
  • 19
  • 17
55

Sync vs Async

Sync and async operations are about execution order a next task in relation to the current task.

Let's take a look at example where Task 2 is current task and Task 3 is a next task. Task is an atomic operation - method call in a stack (method frame).

Synchronous

Implies that tasks will be executed one by one. A next task is started only after current task is finished. Task 3 is not started until Task 2 is finished.

Single Thread + Sync - Sequential

Usual execution.

Pseudocode:

main() {
    task1()
    task2()
    task3()
}

Multi Thread + Sync - Parallel

Blocked.

Blocked means that a thread is just waiting(although it could do something useful. e.g. Java ExecutorService[About] and Future[About]) Pseudocode:

main() {
    task1()
    Future future = ExecutorService.submit(task2())
    future.get() //<- blocked operation
    task3()
}

Asynchronous

Implies that task returns control immediately with a promise to execute a code and notify about result later(e.g. callback, feature). Task 3 is executed even if Task 2 is not finished. async callback, completion handler[About]

Single Thread + Async - Concurrent

Callback Queue (Message Queue) and Event Loop (Run Loop, Looper) are used. Event Loop checks if Thread Stack is empty and if it is true it pushes first item from the Callback Queue into Thread Stack and repeats these steps again. Simple examples are button click, post event...

Pseudocode:

main() {
    task1()
    ThreadMain.handler.post(task2());
    task3()
}

Multi Thread + Async - Concurrent and Parallel

Non-blocking.

For example when you need to make some calculations on another thread without blocking. Pseudocode:

main() {
    task1()

    new Thread(task2()).start();
    //or
    Future future = ExecutorService.submit(task2())

    task3()
}

You are able use result of Task 2 using a blocking method get() or using async callback through a loop.

For example in Mobile world where we have UI/main thread and we need to download something we have several options:

  • sync block - block UI thread and wait when downloading is done. UI is not responsive.
  • async callback - create a new tread with a async callback to update UI(is not possible to access UI from non UI thread). Callback hell.
  • async coroutine[About] - async task with sync syntax. It allows mix downloading task (suspend function) with UI task.

[iOS sync/async], [Android sync/async]

[Paralel vs Concurrent]

VLAZ
  • 26,331
  • 9
  • 49
  • 67
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
  • The diagram in the synchronous multi-threaded example appears to depict concurrently executing threads? – samus May 30 '18 at 19:11
  • @sαmosΛris you can consider Thread like X axis that indicates timeline – yoAlex5 May 30 '18 at 19:33
  • The x-axis is conventionally used as a spacial dimension; the t-axis for time - an important distinction found in the analysis of algorithm complexity. – samus May 30 '18 at 20:54
35

I think this is bit round-about explanation but still it clarifies using real life example.

Small Example:

Let's say playing an audio involves three steps:

  1. Getting the compressed song from harddisk
  2. Decompress the audio.
  3. Play the uncompressed audio.

If your audio player does step 1,2,3 sequentially for every song then it is synchronous. You will have to wait for some time to hear the song till the song actually gets fetched and decompressed.

If your audio player does step 1,2,3 independent of each other, then it is asynchronous. ie. While playing audio 1 ( step 3), if it fetches audio 3 from harddisk in parallel (step 1) and it decompresses the audio 2 in parallel. (step 2 ) You will end up in hearing the song without waiting much for fetch and decompress.

user207421
  • 305,947
  • 44
  • 307
  • 483
aJ.
  • 34,624
  • 22
  • 86
  • 128
33

I created a gif for explain this, hope to be helpful: look, line 3 is asynchronous and others are synchronous. all lines before line 3 should wait until before line finish its work, but because of line 3 is asynchronous, next line (line 4), don't wait for line 3, but line 5 should wait for line 4 to finish its work, and line 6 should wait for line 5 and 7 for 6, because line 4,5,6,7 are not asynchronous. line 3 is asynchronous and others are synchronous

Abolfazl Miadian
  • 3,099
  • 2
  • 19
  • 15
29

Simply said asynchronous execution is doing stuff in the background.

For example if you want to download a file from the internet you might use a synchronous function to do that but it will block your thread until the file finished downloading. This can make your application unresponsive to any user input.

Instead you could download the file in the background using asynchronous method. In this case the download function returns immediately and program execution continues normally. All the download operations are done in the background and your program will be notified when it's finished.

Michał Piaskowski
  • 3,800
  • 2
  • 34
  • 46
  • 2
    how is your example gonna be faster. In the end you can't play the file until it is done downloading period. Can you explain? I guess I don't understand async then and it's probably me but what would that other step or process be doing while the other process is running (getting the download)? I mean what can you do until you receive that async process back (download) in your app...I don't get it. So what, you'd still have to show the user some kind of wait mechanism no mater what in either situation? – PositiveGuy Dec 10 '12 at 05:48
  • 5
    It doesn't have to faster. It's about not blocking the main thread, so that it can process other kind of user input. For example user might want to cancel the download or start downloading another file simultaneously. – Michał Piaskowski Dec 10 '12 at 15:19
23

As a really simple example,

SYNCHRONOUS

Imagine 3 school students instructed to run a relay race on a road.

1st student runs her given distance, stops and passes the baton to the 2nd. No one else has started to run.

1------>
        2.
                3.

When the 2nd student retrieves the baton, she starts to run her given distance.

      1.
        2------>
                3.

The 2nd student got her shoelace untied. Now she has stopped and tying up again. Because of this, 2nd's end time has got extended and the 3rd's starting time has got delayed.

      1.
        --2.--->
                3.

This pattern continues on till the 3rd retrieves the baton from 2nd and finishes the race.

ASYNCHRONOUS

Just Imagine 10 random people walking on the same road. They're not on a queue of course, just randomly walking on different places on the road in different paces.

2nd person's shoelace got untied. She stopped to get it tied up again.

But nobody is waiting for her to get it tied up. Everyone else is still walking the same way they did before, in that same pace of theirs.

10-->    9-->
   8--> 7-->   6-->
 5-->     4-->
1-->   2.    3-->
Dasun Nirmitha
  • 448
  • 1
  • 8
  • 14
18

Synchronous basically means that you can only execute one thing at a time. Asynchronous means that you can execute multiple things at a time and you don't have to finish executing the current thing in order to move on to next one.

Mike
  • 189
  • 1
  • 2
9

When executing a sequence like: a>b>c>d>, if we get a failure in the middle of execution like:

a
b
c
fail

Then we re-start from the beginning:

a
b
c
d

this is synchronous

If, however, we have the same sequence to execute: a>b>c>d>, and we have a failure in the middle:

a
b
c
fail

...but instead of restarting from the beginning, we re-start from the point of failure:

c
d

...this is know as asynchronous.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
mohamed tharik
  • 179
  • 1
  • 2
8

An example of instructions for making a breakfast:

  1. Pour a cup of coffee.
  2. Heat a pan, then fry two eggs.
  3. Fry three slices of bacon.
  4. Toast two pieces of bread.
  5. Add butter and jam to the toast.
  6. Pour a glass of orange juice.
  7. If you have experience with cooking, you'd execute those instructions asynchronously. You'd start warming the pan for eggs, then start the bacon. You'd put the bread in the toaster, then start the eggs. At each step of the process, you'd start a task, then turn your attention to tasks that are ready for your attention.

Cooking breakfast is a good example of asynchronous work that isn't parallel. One person (or thread) can handle all these tasks. Continuing the breakfast analogy, one person can make breakfast asynchronously by starting the next task before the first task completes. The cooking progresses whether or not someone is watching it. As soon as you start warming the pan for the eggs, you can begin frying the bacon. Once the bacon starts, you can put the bread into the toaster.

For a parallel algorithm, you'd need multiple cooks (or threads). One would make the eggs, one the bacon, and so on. Each one would be focused on just that one task. Each cook (or thread) would be blocked synchronously waiting for the bacon to be ready to flip, or the toast to pop.

(emphasis mine)

From Asynchronous programming concepts

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Hla Min Swe
  • 291
  • 3
  • 5
6

A synchronous operation does its work before returning to the caller.

An asynchronous operation does (most or all of) its work after returning to the caller.

HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37
Maxim Eliseev
  • 3,248
  • 4
  • 29
  • 35
2

You are confusing Synchronous with Parallel vs Series. Synchronous mean all at the same time. Syncronized means related to each othere which can mean in series or at a fixed interval. While the program is doing all, it it running in series. Get a dictionary...this is why we have unsweet tea. You have tea or sweetened tea.

Joe
  • 85
  • 1
  • 1
  • 4
    Actually, "synchronized" refers to the relationship between the instructions and the clock. NOT the relationship between the instructions themselves. That's why it looks backwards "synchronous" actually means one after another: but the instructions are SYNCHRONIZED to the clock. "Asynchronous" means "at any time, I don't care when it happens": the instructions need not be synchronized to the clock. Yes, there is a dictionary definition, but you must make sure you are defining the correct situation. – Tom Padilla Jan 28 '16 at 15:41
  • 4
    Synchronous does *not* mean 'all at the same time' in computing. You are confusing synchronization with synchronous, and 'parallel versus series' with tea and sweet tea. Answer makes no sense whatsoever. – user207421 Aug 11 '16 at 04:09
2

A different english definition of Synchronize is Here

Coordinate; combine.

I think that is a better definition than of "Happening at the same time". That one is also a definition, but I don't think it is the one that fits the way it is used in Computer Science.

So an asynchronous task is not co-coordinated with other tasks, whereas a synchronous task IS co-coordinated with other tasks, so one finishes before another starts.

How that is achieved is a different question.

Greg Gum
  • 33,478
  • 39
  • 162
  • 233
1

I think a good way to think of it is a classic running Relay Race

Synchronous: Processes like members of the same team, they won't execute until they receive baton (end of the execution of previous process/runner) and yet they are all acting in sync with each other.

Asynchronous: Where processes like members of different teams on the same relay race track, they will run and stop, async with each other, but within same race (overall program execution).

Does it make sense?

Sharif
  • 343
  • 3
  • 12
0

Synchronous means queue way execution one by one task will be executed. Suppose there is only vehicle that need to be share among friend to reach their destination one by one vehicle will be share.
In asynchronous case each friend can get rented vehicle and reach its destination.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Rohit
  • 142
  • 8
-1

In regards to the "at the same time" definition of synchronous execution (which is sometimes confusing), here's a good way to understand it:

Synchronous Execution: All tasks within a block of code are all executed at the same time.

Asynchronous Execution: All tasks within a block of code are not all executed at the same time.

docta_faustus
  • 2,383
  • 4
  • 30
  • 47
  • I would agree with this more if you said 'effectively at the same time', or 'for practical purposes'.. I think the downvote was for the inaccurate statement that things *actually* are getting done at the same tiem. – Dean Radcliffe Dec 28 '16 at 15:27
-3

Yes synchronous means at the same time, literally, it means doing work all together. multiple human/objects in the world can do multiple things at the same time but if we look at computer, it says synchronous means where the processes work together that means the processes are dependent on the return of one another and that's why they get executed one after another in proper sequence. Whereas asynchronous means where processes don't work together, they may work at the same time(if are on multithread), but work independently.

Saptarshi
  • 125
  • 2
  • 3